Advertisement
emcniece

UAM UserAccessManager.class.php 1.2.5.0 - Allow HTML Patch

Dec 16th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 69.94 KB | None | 0 0
  1. <?php /* http://wordpress.org/support/topic/patch-html-character-entry-for-alternate-postpage-content */ ?>
  2. <?php
  3. /**
  4.  * UserAccessManager.class.php
  5.  *
  6.  * The UserAccessManager class file.
  7.  *
  8.  * PHP versions 5
  9.  *
  10.  * @category  UserAccessManager
  11.  * @package   UserAccessManager
  12.  * @author    Alexander Schneider <alexanderschneider85@googlemail.com>
  13.  * @copyright 2008-2013 Alexander Schneider
  14.  * @license   http://www.gnu.org/licenses/gpl-2.0.html  GNU General Public License, version 2
  15.  * @version   SVN: $Id$
  16.  * @link      http://wordpress.org/extend/plugins/user-access-manager/
  17.  */
  18.  
  19. /**
  20.  * The user user access manager class.
  21.  *
  22.  * @category UserAccessManager
  23.  * @package  UserAccessManager
  24.  * @author   Alexander Schneider <alexanderschneider85@gmail.com>
  25.  * @license  http://www.gnu.org/licenses/gpl-2.0.html  GNU General Public License, version 2
  26.  * @link     http://wordpress.org/extend/plugins/user-access-manager/
  27.  */
  28. class UserAccessManager
  29. {
  30.     protected $_blAtAdminPanel = false;
  31.     protected $_sAdminOptionsName = "uamAdminOptions";
  32.     protected $_sUamVersion = "1.2.5.0";
  33.     protected $_sUamDbVersion = "1.1";
  34.     protected $_aAdminOptions = null;
  35.     protected $_oAccessHandler = null;
  36.     protected $_aPostUrls = array();
  37.     protected $_aMimeTypes = null;
  38.     protected $_aCache = array();
  39.     protected $_aPosts = array();
  40.     protected $_aCategories = array();
  41.     protected $_aWpOptions = array();
  42.    
  43.     /**
  44.      * Constructor.
  45.      */
  46.     public function __construct()
  47.     {
  48.         do_action('uam_init', $this);
  49.     }
  50.  
  51.     /**
  52.      * Returns the admin options name for the uam.
  53.      *
  54.      * @return string
  55.      */
  56.     public function getAdminOptionsName()
  57.     {
  58.         return $this->_sAdminOptionsName;
  59.     }
  60.  
  61.     /**
  62.      * Adds the variable to the cache.
  63.      *
  64.      * @param string $sKey   The cache key
  65.      * @param mixed  $mValue The value.
  66.      */
  67.     public function addToCache($sKey, $mValue)
  68.     {
  69.         $this->_aCache[$sKey] = $mValue;
  70.     }
  71.  
  72.     /**
  73.      * Returns a value from the cache by the given key.
  74.      *
  75.      * @param string $sKey
  76.      *
  77.      * @return mixed
  78.      */
  79.     public function getFromCache($sKey)
  80.     {
  81.         if (isset($this->_aCache[$sKey])) {
  82.             return $this->_aCache[$sKey];
  83.         }
  84.  
  85.         return null;
  86.     }
  87.  
  88.     public function getWpOption($sOption)
  89.     {
  90.         if (!isset($this->_aWpOptions[$sOption])) {
  91.             $this->_aWpOptions[$sOption] = get_option($sOption);
  92.         }
  93.  
  94.         return $this->_aWpOptions[$sOption];
  95.     }
  96.  
  97.     /**
  98.      * Returns a post.
  99.      *
  100.      * @param string $sId The post id.
  101.      *
  102.      * @return mixed
  103.      */
  104.     public function getPost($sId)
  105.     {
  106.         if (!isset($this->_aPosts[$sId])) {
  107.             $this->_aPosts[$sId] = get_post($sId);
  108.         }
  109.  
  110.         return $this->_aPosts[$sId];
  111.     }
  112.  
  113.     /**
  114.      * Returns a category.
  115.      *
  116.      * @param string $sId The category id.
  117.      *
  118.      * @return mixed
  119.      */
  120.     public function getCategory($sId)
  121.     {
  122.         if (!isset($this->_aCategories[$sId])) {
  123.             $this->_aCategories[$sId] = get_category($sId);
  124.         }
  125.  
  126.         return $this->_aCategories[$sId];
  127.     }
  128.    
  129.     /**
  130.      * Returns all blog of the network.
  131.      *
  132.      * @return array()
  133.      */
  134.     protected function _getBlogIds()
  135.     {
  136.         /**
  137.          * @var wpdb $wpdb
  138.          */
  139.         global $wpdb;
  140.         $aBlogIds = array();
  141.  
  142.         if (is_multisite()) {
  143.             $aBlogIds = $wpdb->get_col(
  144.                 "SELECT blog_id
  145.                 FROM ".$wpdb->blogs
  146.             );
  147.         }
  148.  
  149.         return $aBlogIds;
  150.     }
  151.    
  152.     /**
  153.      * Installs the user access manager.
  154.      *
  155.      * @return null;
  156.      */
  157.     public function install()
  158.     {
  159.         global $wpdb;
  160.         $aBlogIds = $this->_getBlogIds();
  161.  
  162.         if (isset($_GET['networkwide'])
  163.             && ($_GET['networkwide'] == 1)
  164.         ) {
  165.             $iCurrentBlogId = $wpdb->blogid;
  166.            
  167.             foreach ($aBlogIds as $iBlogId) {
  168.                 switch_to_blog($iBlogId);
  169.                 $this->_installUam();
  170.             }
  171.            
  172.             switch_to_blog($iCurrentBlogId);
  173.            
  174.             return null;
  175.         }
  176.        
  177.         $this->_installUam();
  178.     }
  179.    
  180.     /**
  181.      * Creates the needed tables at the database and adds the options
  182.      *
  183.      * @return null;
  184.      */
  185.     protected function _installUam()
  186.     {
  187.         /**
  188.          * @var wpdb $wpdb
  189.          */
  190.         global $wpdb;
  191.         include_once ABSPATH.'wp-admin/includes/upgrade.php';
  192.  
  193.         $sCharsetCollate = $this->_getCharset();
  194.        
  195.         $sDbAccessGroupTable = $wpdb->prefix.'uam_accessgroups';
  196.        
  197.         $sDbUserGroup = $wpdb->get_var(
  198.             "SHOW TABLES
  199.             LIKE '".$sDbAccessGroupTable."'"
  200.         );
  201.        
  202.         if ($sDbUserGroup != $sDbAccessGroupTable) {
  203.             dbDelta(
  204.                 "CREATE TABLE ".$sDbAccessGroupTable." (
  205.                     ID int(11) NOT NULL auto_increment,
  206.                     groupname tinytext NOT NULL,
  207.                     groupdesc text NOT NULL,
  208.                     read_access tinytext NOT NULL,
  209.                     write_access tinytext NOT NULL,
  210.                     ip_range mediumtext NULL,
  211.                     PRIMARY KEY  (ID)
  212.                 ) $sCharsetCollate;"
  213.             );
  214.         }
  215.  
  216.         $sDbAccessGroupToObjectTable = $wpdb->prefix.'uam_accessgroup_to_object';
  217.  
  218.         $sDbAccessGroupToObject = $wpdb->get_var(
  219.             "SHOW TABLES
  220.             LIKE '".$sDbAccessGroupToObjectTable."'"
  221.         );
  222.        
  223.         if ($sDbAccessGroupToObject != $sDbAccessGroupToObjectTable) {
  224.             dbDelta(
  225.                 "CREATE TABLE " . $sDbAccessGroupToObjectTable . " (
  226.                     object_id VARCHAR(11) NOT NULL,
  227.                     object_type varchar(255) NOT NULL,
  228.                     group_id int(11) NOT NULL,
  229.                     PRIMARY KEY  (object_id,object_type,group_id)
  230.                 ) $sCharsetCollate;"
  231.             );
  232.         }
  233.        
  234.         add_option("uam_db_version", $this->_sUamDbVersion);
  235.     }
  236.    
  237.     /**
  238.      * Checks if a database update is necessary.
  239.      *
  240.      * @return boolean
  241.      */
  242.     public function isDatabaseUpdateNecessary()
  243.     {
  244.         global $wpdb;
  245.         $sBlogIds = $this->_getBlogIds();
  246.  
  247.         if ($sBlogIds !== array()
  248.             && is_super_admin()
  249.         ) {
  250.             $iCurrentBlogId = $wpdb->blogid;
  251.            
  252.             foreach ($sBlogIds as $iBlogId) {
  253.                 switch_to_blog($iBlogId);
  254.                 $sCurrentDbVersion = $this->getWpOption("uam_db_version");
  255.                
  256.                 if (version_compare($sCurrentDbVersion, $this->_sUamDbVersion, '<')) {
  257.                     switch_to_blog($iCurrentBlogId);
  258.                     return true;
  259.                 }
  260.             }
  261.            
  262.             switch_to_blog($iCurrentBlogId);
  263.         }
  264.        
  265.         $sCurrentDbVersion = $this->getWpOption("uam_db_version");
  266.         return version_compare($sCurrentDbVersion, $this->_sUamDbVersion, '<');
  267.     }
  268.    
  269.     /**
  270.      * Updates the user access manager if an old version was installed.
  271.      *
  272.      * @param boolean $blNetworkWide If true update network wide
  273.      *
  274.      * @return null;
  275.      */
  276.     public function update($blNetworkWide)
  277.     {
  278.         global $wpdb;
  279.         $aBlogIds = $this->_getBlogIds();
  280.  
  281.         if ($aBlogIds !== array()
  282.             && $blNetworkWide
  283.         ) {
  284.             $iCurrentBlogId = $wpdb->blogid;
  285.            
  286.             foreach ($aBlogIds as $iBlogId) {
  287.                 switch_to_blog($iBlogId);
  288.                 $this->_installUam();
  289.             }
  290.            
  291.             switch_to_blog($iCurrentBlogId);
  292.            
  293.             return;
  294.         }
  295.        
  296.         $this->_updateUam();
  297.     }
  298.    
  299.     /**
  300.      * Updates the user access manager if an old version was installed.
  301.      *
  302.      * @return null;
  303.      */
  304.     protected function _updateUam()
  305.     {
  306.         /**
  307.          * @var wpdb $wpdb
  308.          */
  309.         global $wpdb;
  310.         $sCurrentDbVersion = $this->getWpOption("uam_db_version");
  311.        
  312.         if (empty($sCurrentDbVersion)) {
  313.             $this->install();
  314.         }
  315.        
  316.         if (!$this->getWpOption('uam_version') || version_compare($this->getWpOption('uam_version'), "1.0") === -1) {
  317.             delete_option('allow_comments_locked');
  318.         }
  319.        
  320.         $sDbAccessGroup = $wpdb->prefix.'uam_accessgroups';
  321.        
  322.         $sDbUserGroup = $wpdb->get_var(
  323.             "SHOW TABLES
  324.             LIKE '".$sDbAccessGroup."'"
  325.         );
  326.        
  327.         if (version_compare($sCurrentDbVersion, $this->_sUamDbVersion) === -1) {
  328.             if (version_compare($sCurrentDbVersion, "1.0") === 0) {
  329.                 if ($sDbUserGroup == $sDbAccessGroup) {
  330.                     $wpdb->query(
  331.                         "ALTER TABLE ".$sDbAccessGroup."
  332.                         ADD read_access TINYTEXT NOT NULL DEFAULT '',
  333.                         ADD write_access TINYTEXT NOT NULL DEFAULT '',
  334.                         ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
  335.                     );
  336.                    
  337.                     $wpdb->query(
  338.                         "UPDATE ".$sDbAccessGroup."
  339.                         SET read_access = 'group',
  340.                             write_access = 'group'"
  341.                     );
  342.                    
  343.                     $sDbIpRange = $wpdb->get_var(
  344.                         "SHOW columns
  345.                         FROM ".$sDbAccessGroup."
  346.                         LIKE 'ip_range'"
  347.                     );
  348.            
  349.                     if ($sDbIpRange != 'ip_range') {
  350.                         $wpdb->query(
  351.                             "ALTER TABLE ".$sDbAccessGroup."
  352.                             ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
  353.                         );
  354.                     }
  355.                 }
  356.  
  357.                 $sCurrentDbVersion = "1.1";
  358.             }
  359.            
  360.             if (version_compare($sCurrentDbVersion, "1.1") === 0) {
  361.                 $sDbAccessGroupToObject = $wpdb->prefix.'uam_accessgroup_to_object';
  362.                 $sDbAccessGroupToPost = $wpdb->prefix.'uam_accessgroup_to_post';
  363.                 $sDbAccessGroupToUser = $wpdb->prefix.'uam_accessgroup_to_user';
  364.                 $sDbAccessGroupToCategory = $wpdb->prefix.'uam_accessgroup_to_category';
  365.                 $sDbAccessGroupToRole = $wpdb->prefix.'uam_accessgroup_to_role';
  366.                
  367.                 $sCharsetCollate = $this->_getCharset();
  368.                
  369.                 $wpdb->query(
  370.                     "ALTER TABLE 'wp_uam_accessgroup_to_object'
  371.                    CHANGE 'object_id' 'object_id' VARCHAR(11)
  372.                    ".$sCharsetCollate.";"
  373.                 );
  374.                
  375.                 $aObjectTypes = $this->getAccessHandler()->getObjectTypes();
  376.                
  377.                 foreach ($aObjectTypes as $sObjectType) {
  378.                     $sAddition = '';
  379.  
  380.                     if ($this->getAccessHandler()->isPostableType($sObjectType)) {
  381.                         $sDbIdName = 'post_id';
  382.                         $sDatabase = $sDbAccessGroupToPost.', '.$wpdb->posts;
  383.                         $sAddition = " WHERE post_id = ID
  384.                             AND post_type = '".$sObjectType."'";
  385.                     } elseif ($sObjectType == 'category') {
  386.                         $sDbIdName = 'category_id';
  387.                         $sDatabase = $sDbAccessGroupToCategory;
  388.                     } elseif ($sObjectType == 'user') {
  389.                         $sDbIdName = 'user_id';
  390.                         $sDatabase = $sDbAccessGroupToUser;
  391.                     } elseif ($sObjectType == 'role') {
  392.                         $sDbIdName = 'role_name';
  393.                         $sDatabase = $sDbAccessGroupToRole;
  394.                     } else {
  395.                         continue;
  396.                     }
  397.                    
  398.                     $sSql = "SELECT ".$sDbIdName." as id, group_id as groupId
  399.                         FROM ".$sDatabase.$sAddition;
  400.                        
  401.                     $aDbObjects = $wpdb->get_results($sSql);
  402.                    
  403.                     foreach ($aDbObjects as $oDbObject) {
  404.                         $sSql = "INSERT INTO ".$sDbAccessGroupToObject." (
  405.                                 group_id,
  406.                                 object_id,
  407.                                 object_type
  408.                             )
  409.                             VALUES(
  410.                                 '".$oDbObject->groupId."',
  411.                                 '".$oDbObject->id."',
  412.                                 '".$sObjectType."'
  413.                             )";
  414.                        
  415.                         $wpdb->query($sSql);
  416.                     }
  417.                 }
  418.                
  419.                 $wpdb->query(
  420.                     "DROP TABLE ".$sDbAccessGroupToPost.",
  421.                         ".$sDbAccessGroupToUser.",
  422.                         ".$sDbAccessGroupToCategory.",
  423.                         ".$sDbAccessGroupToRole
  424.                 );
  425.             }
  426.            
  427.             update_option('uam_db_version', $this->_sUamDbVersion);
  428.         }
  429.     }    
  430.    
  431.     /**
  432.      * Clean up wordpress if the plugin will be uninstalled.
  433.      *
  434.      * @return null
  435.      */
  436.     public function uninstall()
  437.     {
  438.         /**
  439.          * @var wpdb $wpdb
  440.          */
  441.         global $wpdb;
  442.  
  443.         $wpdb->query(
  444.             "DROP TABLE ".DB_ACCESSGROUP.",
  445.                 ".DB_ACCESSGROUP_TO_OBJECT
  446.         );
  447.        
  448.         delete_option($this->_sAdminOptionsName);
  449.         delete_option('uam_version');
  450.         delete_option('uam_db_version');
  451.         $this->deleteHtaccessFiles();
  452.     }
  453.    
  454.     /**
  455.      * Returns the database charset.
  456.      *
  457.      * @return string
  458.      */
  459.     protected function _getCharset()
  460.     {
  461.         global $wpdb;
  462.         $sCharsetCollate = '';
  463.        
  464.         if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
  465.             if (!empty($wpdb->charset)) {
  466.                 $sCharsetCollate = "DEFAULT CHARACTER SET $wpdb->charset";
  467.             }
  468.            
  469.             if (!empty($wpdb->collate)) {
  470.                 $sCharsetCollate.= " COLLATE $wpdb->collate";
  471.             }
  472.         }
  473.        
  474.         return $sCharsetCollate;
  475.     }
  476.    
  477.     /**
  478.      * Remove the htaccess file if the plugin is deactivated.
  479.      *
  480.      * @return null
  481.      */
  482.     public function deactivate()
  483.     {
  484.         $this->deleteHtaccessFiles();
  485.     }
  486.  
  487.     /**
  488.      * Returns the current user.
  489.      *
  490.      * @return WP_User
  491.      */
  492.     public function getCurrentUser()
  493.     {
  494.         if (!function_exists('get_userdata')) {
  495.             include_once ABSPATH.'wp-includes/pluggable.php';
  496.         }
  497.  
  498.         //Force user information
  499.         return wp_get_current_user();
  500.     }
  501.  
  502.     /**
  503.      * Returns the full supported mine types.
  504.      *
  505.      * @return array
  506.      */
  507.     protected function _getMimeTypes()
  508.     {
  509.         if ($this->_aMimeTypes === null) {
  510.             $aMimeTypes = get_allowed_mime_types();
  511.             $aFullMimeTypes = array();
  512.  
  513.             foreach ($aMimeTypes as $sExtensions => $sMineType) {
  514.                 $aExtension = explode('|', $sExtensions);
  515.  
  516.                 foreach ($aExtension as $sExtension) {
  517.                     $aFullMimeTypes[$sExtension] = $sMineType;
  518.                 }
  519.             }
  520.  
  521.             $this->_aMimeTypes = $aFullMimeTypes;
  522.         }
  523.  
  524.         return $this->_aMimeTypes;
  525.     }
  526.  
  527.     /**
  528.      * @param string $sFileTypes The file types which should be cleaned up.
  529.      *
  530.      * @return string
  531.      */
  532.     protected function _cleanUpFileTypesForHtaccess($sFileTypes)
  533.     {
  534.         $aValidFileTypes = array();
  535.         $aFileTypes = explode(',', $sFileTypes);
  536.         $aMimeTypes = $this->_getMimeTypes();
  537.  
  538.         foreach ($aFileTypes as $sFileType) {
  539.             $sCleanFileType = trim($sFileType);
  540.  
  541.             if (isset($aMimeTypes[$sCleanFileType])) {
  542.                 $aValidFileTypes[$sCleanFileType] = $sCleanFileType;
  543.             }
  544.         }
  545.  
  546.         return implode('|', $aValidFileTypes);
  547.     }
  548.    
  549.     /**
  550.      * Creates a htaccess file.
  551.      *
  552.      * @param string $sDir        The destination directory.
  553.      * @param string $sObjectType The object type.
  554.      *
  555.      * @return null.
  556.      */
  557.     public function createHtaccess($sDir = null, $sObjectType = null)
  558.     {
  559.         if ($sDir === null) {
  560.             $aWordpressUploadDir = wp_upload_dir();
  561.            
  562.             if (empty($aWordpressUploadDir['error'])) {
  563.                 $sDir = $aWordpressUploadDir['basedir'] . "/";
  564.             }
  565.         }
  566.        
  567.         if ($sObjectType === null) {
  568.             $sObjectType = 'attachment';
  569.         }
  570.        
  571.         if ($sDir !== null) {
  572.             if (!$this->isPermalinksActive()) {
  573.                 $sAreaName = "WP-Files";
  574.                 $aUamOptions = $this->getAdminOptions();
  575.    
  576.                 // make .htaccess and .htpasswd
  577.                 $sHtaccessTxt = "";
  578.                
  579.                 if ($aUamOptions['lock_file_types'] == 'selected') {
  580.                     $sFileTypes = $this->_cleanUpFileTypesForHtaccess($aUamOptions['locked_file_types']);
  581.                     $sHtaccessTxt .= "<FilesMatch '\.(".$sFileTypes.")'>\n";
  582.                 } elseif ($aUamOptions['lock_file_types'] == 'not_selected') {
  583.                     $sFileTypes = $this->_cleanUpFileTypesForHtaccess($aUamOptions['not_locked_file_types']);
  584.                     $sHtaccessTxt .= "<FilesMatch '^\.(".$sFileTypes.")'>\n";
  585.                 }
  586.  
  587.                 $sHtaccessTxt .= "AuthType Basic" . "\n";
  588.                 $sHtaccessTxt .= "AuthName \"" . $sAreaName . "\"" . "\n";
  589.                 $sHtaccessTxt .= "AuthUserFile " . $sDir . ".htpasswd" . "\n";
  590.                 $sHtaccessTxt .= "require valid-user" . "\n";
  591.                
  592.                 if ($aUamOptions['lock_file_types'] == 'selected'
  593.                     || $aUamOptions['lock_file_types'] == 'not_selected'
  594.                 ) {
  595.                     $sHtaccessTxt.= "</FilesMatch>\n";
  596.                 }
  597.             } else {
  598.                 $aHomeRoot = parse_url(home_url());
  599.                 if (isset($aHomeRoot['path'])) {
  600.                     $aHomeRoot = trailingslashit($aHomeRoot['path']);
  601.                 } else {
  602.                     $aHomeRoot = '/';
  603.                 }
  604.                
  605.                 $sHtaccessTxt = "<IfModule mod_rewrite.c>\n";
  606.                 $sHtaccessTxt .= "RewriteEngine On\n";
  607.                 $sHtaccessTxt .= "RewriteBase ".$aHomeRoot."\n";
  608.                 $sHtaccessTxt .= "RewriteRule ^index\.php$ - [L]\n";
  609.                 $sHtaccessTxt .= "RewriteRule (.*) ";
  610.                 $sHtaccessTxt .= $aHomeRoot."index.php?uamfiletype=".$sObjectType."&uamgetfile=$1 [L]\n";
  611.                 $sHtaccessTxt .= "</IfModule>\n";
  612.             }
  613.            
  614.             // save files
  615.             $oFileHandler = fopen($sDir.".htaccess", "w");
  616.             fwrite($oFileHandler, $sHtaccessTxt);
  617.             fclose($oFileHandler);
  618.         }
  619.     }
  620.    
  621.     /**
  622.      * Creates a htpasswd file.
  623.      *
  624.      * @param boolean $blCreateNew Force to create new file.
  625.      * @param string  $sDir       The destination directory.
  626.      *
  627.      * @return null
  628.      */
  629.     public function createHtpasswd($blCreateNew = false, $sDir = null)
  630.     {
  631.         $oCurrentUser = $this->getCurrentUser();
  632.         if (!function_exists('get_userdata')) {
  633.             include_once ABSPATH.'wp-includes/pluggable.php';
  634.         }
  635.        
  636.         $aUamOptions = $this->getAdminOptions();
  637.  
  638.         // get url
  639.         if ($sDir === null) {
  640.             $aWordpressUploadDir = wp_upload_dir();
  641.            
  642.             if (empty($aWordpressUploadDir['error'])) {
  643.                 $sDir = $aWordpressUploadDir['basedir'] . "/";
  644.             }
  645.         }
  646.        
  647.         if ($sDir !== null) {
  648.             $oUserData = get_userdata($oCurrentUser->ID);
  649.            
  650.             if (!file_exists($sDir.".htpasswd") || $blCreateNew) {
  651.                 if ($aUamOptions['file_pass_type'] == 'random') {
  652.                     $sPassword = md5($this->getRandomPassword());
  653.                 } else {
  654.                     $sPassword = $oUserData->user_pass;
  655.                 }
  656.              
  657.                 $sUser = $oUserData->user_login;
  658.  
  659.                 // make .htpasswd
  660.                 $sHtpasswdTxt = "$sUser:" . $sPassword . "\n";
  661.                
  662.                 // save file
  663.                 $oFileHandler = fopen($sDir.".htpasswd", "w");
  664.                 fwrite($oFileHandler, $sHtpasswdTxt);
  665.                 fclose($oFileHandler);
  666.             }
  667.         }
  668.     }
  669.    
  670.     /**
  671.      * Deletes the htaccess files.
  672.      *
  673.      * @param string $sDir The destination directory.
  674.      *
  675.      * @return null
  676.      */
  677.     public function deleteHtaccessFiles($sDir = null)
  678.     {
  679.         if ($sDir === null) {
  680.             $aWordpressUploadDir = wp_upload_dir();
  681.            
  682.             if (empty($aWordpressUploadDir['error'])) {
  683.                 $sDir = $aWordpressUploadDir['basedir'] . "/";
  684.             }
  685.         }
  686.  
  687.         if ($sDir !== null) {
  688.             if (file_exists($sDir.".htaccess")) {
  689.                 unlink($sDir.".htaccess");
  690.             }
  691.            
  692.             if (file_exists($sDir.".htpasswd")) {
  693.                 unlink($sDir.".htpasswd");
  694.             }
  695.         }
  696.     }
  697.    
  698.     /**
  699.      * Generates and returns a random password.
  700.      *
  701.      * @return string
  702.      */
  703.     public function getRandomPassword()
  704.     {
  705.         //create password
  706.         $aArray = array();
  707.         $iLength = 16;
  708.  
  709.         // numbers
  710.         for ($i = 48; $i < 58; $i++) {
  711.             $aArray[] = chr($i);
  712.         }
  713.  
  714.         // small
  715.         for ($i = 97; $i < 122; $i++) {
  716.             $aArray[] = chr($i);
  717.         }
  718.  
  719.         // capitals
  720.         for ($i = 65; $i < 90; $i++) {
  721.             $aArray[] = chr($i);
  722.         }
  723.        
  724.         mt_srand((double)microtime() * 1000000);
  725.         $sPassword = '';
  726.        
  727.         for ($i = 1; $i <= $iLength; $i++) {
  728.             $iRandomNumber = mt_rand(0, count($aArray) - 1);
  729.             $sPassword .= $aArray[$iRandomNumber];
  730.         }
  731.        
  732.         return $sPassword;
  733.     }
  734.    
  735.     /**
  736.      * Returns the current settings
  737.      *
  738.      * @return array
  739.      */
  740.     public function getAdminOptions()
  741.     {
  742.         if ($this->_aAdminOptions === null) {
  743.             $aUamAdminOptions = array(
  744.                 'hide_post_title' => 'false',
  745.                 'post_title' => __('No rights!', 'user-access-manager'),
  746.                 'post_content' => __(
  747.                     'Sorry you have no rights to view this post!',
  748.                     'user-access-manager'
  749.                 ),
  750.                 'hide_post' => 'false',
  751.                 'hide_post_comment' => 'false',
  752.                 'post_comment_content' => __(
  753.                     'Sorry no rights to view comments!',
  754.                     'user-access-manager'
  755.                 ),
  756.                 'post_comments_locked' => 'false',
  757.                 'hide_page_title' => 'false',
  758.                 'page_title' => __('No rights!', 'user-access-manager'),
  759.                 'page_content' => __(
  760.                     'Sorry you have no rights to view this page!',
  761.                     'user-access-manager'
  762.                 ),
  763.                 'hide_page' => 'false',
  764.                 'hide_page_comment' => 'false',
  765.                 'page_comment_content' => __(
  766.                     'Sorry no rights to view comments!',
  767.                     'user-access-manager'
  768.                 ),
  769.                 'page_comments_locked' => 'false',
  770.                 'redirect' => 'false',
  771.                 'redirect_custom_page' => '',
  772.                 'redirect_custom_url' => '',
  773.                 'lock_recursive' => 'true',
  774.                 'authors_has_access_to_own' => 'true',
  775.                 'authors_can_add_posts_to_groups' => 'false',
  776.                 'lock_file' => 'false',
  777.                 'file_pass_type' => 'random',
  778.                 'lock_file_types' => 'all',
  779.                 'download_type' => 'fopen',
  780.                 'locked_file_types' => 'zip,rar,tar,gz',
  781.                 'not_locked_file_types' => 'gif,jpg,jpeg,png',
  782.                 'blog_admin_hint' => 'true',
  783.                 'blog_admin_hint_text' => '[L]',
  784.                 'hide_empty_categories' => 'true',
  785.                 'protect_feed' => 'true',
  786.                 'show_post_content_before_more' => 'false',
  787.                 'full_access_role' => 'administrator'
  788.             );
  789.            
  790.             $aUamOptions = $this->getWpOption($this->_sAdminOptionsName);
  791.            
  792.             if (!empty($aUamOptions)) {
  793.                 foreach ($aUamOptions as $sKey => $mOption) {
  794.                     $aUamAdminOptions[$sKey] = $mOption;
  795.                 }
  796.             }
  797.            
  798.             update_option($this->_sAdminOptionsName, $aUamAdminOptions);
  799.             $this->_aAdminOptions = $aUamAdminOptions;
  800.         }
  801.  
  802.         return $this->_aAdminOptions;
  803.     }
  804.  
  805.     /**
  806.      * Returns the content of the excluded php file.
  807.      *
  808.      * @param string  $sFileName   The file name
  809.      * @param integer $iObjectId   The _iId if needed.
  810.      * @param string  $sObjectType The object type if needed.
  811.      *
  812.      * @return string
  813.      */
  814.     public function getIncludeContents($sFileName, $iObjectId = null, $sObjectType = null)
  815.     {
  816.         if (is_file($sFileName)) {
  817.             ob_start();
  818.             include $sFileName;
  819.             $sContents = ob_get_contents();
  820.             ob_end_clean();
  821.            
  822.             return $sContents;
  823.         }
  824.        
  825.         return '';
  826.     }
  827.    
  828.     /**
  829.      * Returns the access handler object.
  830.      *
  831.      * @return UamAccessHandler
  832.      */
  833.     public function &getAccessHandler()
  834.     {
  835.         if ($this->_oAccessHandler == null) {
  836.             $this->_oAccessHandler = new UamAccessHandler($this);
  837.         }
  838.        
  839.         return $this->_oAccessHandler;
  840.     }
  841.    
  842.     /**
  843.      * Returns the current version of the user access manager.
  844.      *
  845.      * @return string
  846.      */
  847.     public function getVersion()
  848.     {
  849.         return $this->_sUamVersion;
  850.     }
  851.    
  852.     /**
  853.      * Returns true if a user is at the admin panel.
  854.      *
  855.      * @return boolean
  856.      */
  857.     public function atAdminPanel()
  858.     {
  859.         return $this->_blAtAdminPanel;
  860.     }
  861.    
  862.     /**
  863.      * Sets the atAdminPanel var to true.
  864.      *
  865.      * @return null
  866.      */
  867.     public function setAtAdminPanel()
  868.     {
  869.         $this->_blAtAdminPanel = true;
  870.     }
  871.    
  872.    
  873.     /*
  874.      * Helper functions.
  875.      */
  876.    
  877.     /**
  878.      * Checks if a string starts with the given needle.
  879.      *
  880.      * @param string $sHaystack The haystack.
  881.      * @param string $sNeedle   The needle.
  882.      *
  883.      * @return boolean
  884.      */
  885.     public function startsWith($sHaystack, $sNeedle)
  886.     {
  887.         return strpos($sHaystack, $sNeedle) === 0;
  888.     }
  889.    
  890.    
  891.     /*
  892.      * Functions for the admin panel content.
  893.      */
  894.    
  895.     /**
  896.      * The function for the wp_print_styles action.
  897.      *
  898.      * @return null
  899.      */
  900.     public function addStyles()
  901.     {
  902.         wp_enqueue_style(
  903.             'UserAccessManagerAdmin',
  904.             UAM_URLPATH . "css/uamAdmin.css",
  905.             array() ,
  906.             '1.0',
  907.             'screen'
  908.         );
  909.        
  910.         wp_enqueue_style(
  911.             'UserAccessManagerLoginForm',
  912.             UAM_URLPATH . "css/uamLoginForm.css",
  913.             array() ,
  914.             '1.0',
  915.             'screen'
  916.         );
  917.     }
  918.    
  919.     /**
  920.      * The function for the wp_print_scripts action.
  921.      *
  922.      * @return null
  923.      */
  924.     public function addScripts()
  925.     {
  926.         wp_enqueue_script(
  927.             'UserAccessManagerJQueryTools',
  928.             UAM_URLPATH . 'js/jquery.tools.min.js',
  929.             array('jquery')
  930.         );
  931.         wp_enqueue_script(
  932.             'UserAccessManagerFunctions',
  933.             UAM_URLPATH . 'js/functions.js',
  934.             array('jquery', 'UserAccessManagerJQueryTools')
  935.         );
  936.     }
  937.    
  938.     /**
  939.      * Prints the admin page.
  940.      *
  941.      * @return null
  942.      */
  943.     public function printAdminPage()
  944.     {
  945.         if (isset($_GET['page'])) {
  946.             $sAdminPage = $_GET['page'];
  947.  
  948.             if ($sAdminPage == 'uam_settings') {
  949.                 include UAM_REALPATH."tpl/adminSettings.php";
  950.             } elseif ($sAdminPage == 'uam_usergroup') {
  951.                 include UAM_REALPATH."tpl/adminGroup.php";
  952.             } elseif ($sAdminPage == 'uam_setup') {
  953.                 include UAM_REALPATH."tpl/adminSetup.php";
  954.             } elseif ($sAdminPage == 'uam_about') {
  955.                 include UAM_REALPATH."tpl/about.php";
  956.             }
  957.         }
  958.     }
  959.    
  960.     /**
  961.      * Shows the error if the user has no rights to edit the content.
  962.      *
  963.      * @return null
  964.      */
  965.     public function noRightsToEditContent()
  966.     {
  967.         $blNoRights = false;
  968.        
  969.         if (isset($_GET['post']) && is_numeric($_GET['post'])) {
  970.             $oPost = $this->getPost($_GET['post']);
  971.             $blNoRights = !$this->getAccessHandler()->checkObjectAccess( $oPost->post_type, $oPost->ID );
  972.         }
  973.        
  974.         if (isset($_GET['attachment_id']) && is_numeric($_GET['attachment_id']) && !$blNoRights) {
  975.             $oPost = $this->getPost($_GET['attachment_id']);
  976.             $blNoRights = !$this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID);
  977.         }
  978.        
  979.         if (isset($_GET['tag_ID']) && is_numeric($_GET['tag_ID']) && !$blNoRights) {
  980.             $blNoRights = !$this->getAccessHandler()->checkObjectAccess('category', $_GET['tag_ID']);
  981.         }
  982.  
  983.         if ($blNoRights) {
  984.             wp_die(TXT_UAM_NO_RIGHTS);
  985.         }
  986.     }
  987.    
  988.     /**
  989.      * The function for the wp_dashboard_setup action.
  990.      * Removes widgets to which a user should not have access.
  991.      *
  992.      * @return null
  993.      */
  994.     public function setupAdminDashboard()
  995.     {
  996.         global $wp_meta_boxes;
  997.        
  998.         if (!$this->getAccessHandler()->checkUserAccess('manage_user_groups')) {
  999.             unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
  1000.         }
  1001.     }
  1002.    
  1003.     /**
  1004.      * The function for the update_option_permalink_structure action.
  1005.      *
  1006.      * @return null
  1007.      */
  1008.     public function updatePermalink()
  1009.     {
  1010.         $this->createHtaccess();
  1011.         $this->createHtpasswd();
  1012.     }
  1013.    
  1014.    
  1015.     /*
  1016.      * Meta functions
  1017.      */
  1018.    
  1019.     /**
  1020.      * Saves the object data to the database.
  1021.      *
  1022.      * @param string  $sObjectType The object type.
  1023.      * @param integer $iObjectId   The _iId of the object.
  1024.      * @param array   $aUserGroups The new usergroups for the object.
  1025.      *
  1026.      * @return null
  1027.      */
  1028.     protected function _saveObjectData($sObjectType, $iObjectId, $aUserGroups = null)
  1029.     {        
  1030.         $oUamAccessHandler = $this->getAccessHandler();
  1031.         $oUamOptions = $this->getAdminOptions();
  1032.         $aFormData = array();
  1033.  
  1034.         if (isset($_POST['uam_update_groups'])) {
  1035.             $aFormData = $_POST;
  1036.         } elseif (isset($_GET['uam_update_groups'])) {
  1037.             $aFormData = $_GET;
  1038.         }
  1039.  
  1040.         if (isset($aFormData['uam_update_groups'])
  1041.             && ($oUamAccessHandler->checkUserAccess('manage_user_groups')
  1042.             || $oUamOptions['authors_can_add_posts_to_groups'] == 'true')
  1043.         ) {
  1044.             if ($aUserGroups === null) {
  1045.                 $aUserGroups = isset($aFormData['uam_usergroups']) ? $aFormData['uam_usergroups'] : array();
  1046.             }
  1047.  
  1048.             $aAddUserGroups = array_flip($aUserGroups);
  1049.             $aRemoveUserGroups = $oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId);
  1050.             $aUamUserGroups = $oUamAccessHandler->getUserGroups();
  1051.             $blRemoveOldAssignments = true;
  1052.  
  1053.             if (isset($aFormData['uam_bulk_type'])) {
  1054.                 $sBulkType = $aFormData['uam_bulk_type'];
  1055.  
  1056.                 if ($sBulkType === 'add') {
  1057.                     $blRemoveOldAssignments = false;
  1058.                 } elseif ($sBulkType === 'remove') {
  1059.                     $aRemoveUserGroups = $aAddUserGroups;
  1060.                     $aAddUserGroups = array();
  1061.                 }
  1062.             }
  1063.  
  1064.             foreach ($aUamUserGroups as $sGroupId => $oUamUserGroup) {
  1065.                 if (isset($aRemoveUserGroups[$sGroupId])) {
  1066.                     $oUamUserGroup->removeObject($sObjectType, $iObjectId);
  1067.                 }
  1068.  
  1069.                 if (isset($aAddUserGroups[$sGroupId])) {
  1070.                     $oUamUserGroup->addObject($sObjectType, $iObjectId);
  1071.                 }
  1072.  
  1073.                 $oUamUserGroup->save($blRemoveOldAssignments);
  1074.             }
  1075.         }
  1076.     }
  1077.    
  1078.    
  1079.     /*
  1080.      * Functions for the post actions.
  1081.      */
  1082.    
  1083.     /**
  1084.      * The function for the manage_posts_columns and
  1085.      * the manage_pages_columns filter.
  1086.      *
  1087.      * @param array $aDefaults The table headers.
  1088.      *
  1089.      * @return array
  1090.      */
  1091.     public function addPostColumnsHeader($aDefaults)
  1092.     {
  1093.         $aDefaults['uam_access'] = __('Access', 'user-access-manager');
  1094.         return $aDefaults;
  1095.     }
  1096.    
  1097.     /**
  1098.      * The function for the manage_users_custom_column action.
  1099.      *
  1100.      * @param string  $sColumnName The column name.
  1101.      * @param integer $iId         The _iId.
  1102.      *
  1103.      * @return string
  1104.      */
  1105.     public function addPostColumn($sColumnName, $iId)
  1106.     {
  1107.         if ($sColumnName == 'uam_access') {
  1108.             $oPost = $this->getPost($iId);
  1109.             echo $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $oPost->ID, $oPost->post_type);
  1110.         }
  1111.     }
  1112.    
  1113.     /**
  1114.      * The function for the uma_post_access metabox.
  1115.      *
  1116.      * @param object $oPost The post.
  1117.      *
  1118.      * @return null;
  1119.      */
  1120.     public function editPostContent($oPost)
  1121.     {
  1122.         $iObjectId = $oPost->ID;
  1123.         include UAM_REALPATH.'tpl/postEditForm.php';
  1124.     }
  1125.  
  1126.     public function addBulkAction($sColumnName)
  1127.     {
  1128.         if ($sColumnName == 'uam_access') {
  1129.             include UAM_REALPATH.'tpl/bulkEditForm.php';
  1130.         }
  1131.     }
  1132.    
  1133.     /**
  1134.      * The function for the save_post action.
  1135.      *
  1136.      * @param mixed $mPostParam The post _iId or a array of a post.
  1137.      *
  1138.      * @return null
  1139.      */    
  1140.     public function savePostData($mPostParam)
  1141.     {
  1142.         if (is_array($mPostParam)) {
  1143.             $oPost = $this->getPost($mPostParam['ID']);
  1144.         } else {
  1145.             $oPost = $this->getPost($mPostParam);
  1146.         }
  1147.        
  1148.         $iPostId = $oPost->ID;
  1149.         $sPostType = $oPost->post_type;
  1150.        
  1151.         if ($sPostType == 'revision') {
  1152.             $iPostId = $oPost->post_parent;
  1153.             $oParentPost = $this->getPost($iPostId);
  1154.             $sPostType = $oParentPost->post_type;
  1155.         }
  1156.        
  1157.         $this->_saveObjectData($sPostType, $iPostId);
  1158.     }
  1159.  
  1160.     /**
  1161.      * The function for the attachment_fields_to_save filter.
  1162.      * We have to use this because the attachment actions work
  1163.      * not in the way we need.
  1164.      *
  1165.      * @param object $oAttachment The attachment _iId.
  1166.      *
  1167.      * @return object
  1168.      */    
  1169.     public function saveAttachmentData($oAttachment)
  1170.     {
  1171.         $this->savePostData($oAttachment['ID']);
  1172.        
  1173.         return $oAttachment;
  1174.     }
  1175.    
  1176.     /**
  1177.      * The function for the delete_post action.
  1178.      *
  1179.      * @param integer $iPostId The post _iId.
  1180.      *
  1181.      * @return null
  1182.      */
  1183.     public function removePostData($iPostId)
  1184.     {
  1185.         /**
  1186.          * @var wpdb $wpdb
  1187.          */
  1188.         global $wpdb;
  1189.         $oPost = $this->getPost($iPostId);
  1190.        
  1191.         $wpdb->query(
  1192.             "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
  1193.             WHERE object_id = '".$iPostId."'
  1194.                 AND object_type = '".$oPost->post_type."'"
  1195.         );
  1196.     }
  1197.    
  1198.     /**
  1199.      * The function for the media_meta action.
  1200.      *
  1201.      * @param string $sMeta The meta.
  1202.      * @param object $oPost The post.
  1203.      *
  1204.      * @return string
  1205.      */
  1206.     public function showMediaFile($sMeta = '', $oPost = null)
  1207.     {  
  1208.         $sContent = $sMeta;
  1209.         $sContent .= '</td></tr><tr>';
  1210.         $sContent .= '<th class="label">';
  1211.         $sContent .= '<label>'.TXT_UAM_SET_UP_USERGROUPS.'</label>';
  1212.         $sContent .= '</th>';
  1213.         $sContent .= '<td class="field">';
  1214.         $sContent .= $this->getIncludeContents(UAM_REALPATH.'tpl/postEditForm.php', $oPost->ID);
  1215.        
  1216.         return $sContent;
  1217.     }
  1218.    
  1219.    
  1220.     /*
  1221.      * Functions for the user actions.
  1222.      */
  1223.    
  1224.     /**
  1225.      * The function for the manage_users_columns filter.
  1226.      *
  1227.      * @param array $aDefaults The table headers.
  1228.      *
  1229.      * @return array
  1230.      */
  1231.     public function addUserColumnsHeader($aDefaults)
  1232.     {
  1233.         $aDefaults['uam_access'] = __('uam user groups');
  1234.         return $aDefaults;
  1235.     }
  1236.    
  1237.     /**
  1238.      * The function for the manage_users_custom_column action.
  1239.      *
  1240.      * @param string  $sReturn     The normal return value.
  1241.      * @param string  $sColumnName The column name.
  1242.      * @param integer $iId         The _iId.
  1243.      *
  1244.      * @return string|null
  1245.      */
  1246.     public function addUserColumn($sReturn, $sColumnName, $iId)
  1247.     {
  1248.         if ($sColumnName == 'uam_access') {
  1249.             return $this->getIncludeContents(UAM_REALPATH.'tpl/userColumn.php', $iId, 'user');
  1250.         }
  1251.  
  1252.         return $sReturn;
  1253.     }
  1254.    
  1255.     /**
  1256.      * The function for the edit_user_profile action.
  1257.      *
  1258.      * @return null
  1259.      */
  1260.     public function showUserProfile()
  1261.     {
  1262.         echo $this->getIncludeContents(UAM_REALPATH.'tpl/userProfileEditForm.php');
  1263.     }
  1264.    
  1265.     /**
  1266.      * The function for the profile_update action.
  1267.      *
  1268.      * @param integer $iUserId The user _iId.
  1269.      *
  1270.      * @return null
  1271.      */
  1272.     public function saveUserData($iUserId)
  1273.     {        
  1274.         $this->_saveObjectData('user', $iUserId);
  1275.     }
  1276.    
  1277.     /**
  1278.      * The function for the delete_user action.
  1279.      *
  1280.      * @param integer $iUserId The user _iId.
  1281.      *
  1282.      * @return null
  1283.      */
  1284.     public function removeUserData($iUserId)
  1285.     {
  1286.         /**
  1287.          * @var wpdb $wpdb
  1288.          */
  1289.         global $wpdb;
  1290.  
  1291.         $wpdb->query(
  1292.             "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
  1293.             WHERE object_id = ".$iUserId."
  1294.                AND object_type = 'user'"
  1295.         );
  1296.     }
  1297.    
  1298.    
  1299.     /*
  1300.      * Functions for the category actions.
  1301.      */
  1302.    
  1303.     /**
  1304.      * The function for the manage_categories_columns filter.
  1305.      *
  1306.      * @param array $aDefaults The table headers.
  1307.      *
  1308.      * @return array
  1309.      */
  1310.     public function addCategoryColumnsHeader($aDefaults)
  1311.     {
  1312.         $aDefaults['uam_access'] = __('Access', 'user-access-manager');
  1313.         return $aDefaults;
  1314.     }
  1315.    
  1316.     /**
  1317.      * The function for the manage_categories_custom_column action.
  1318.      *
  1319.      * @param string  $sEmpty      An empty string from wordpress? What the hell?!?
  1320.      * @param string  $sColumnName The column name.
  1321.      * @param integer $iId         The _iId.
  1322.      *
  1323.      * @return string|null
  1324.      */
  1325.     public function addCategoryColumn($sEmpty, $sColumnName, $iId)
  1326.     {
  1327.         if ($sColumnName == 'uam_access') {
  1328.             return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iId, 'category');
  1329.         }
  1330.  
  1331.         return null;
  1332.     }
  1333.    
  1334.     /**
  1335.      * The function for the edit_category_form action.
  1336.      *
  1337.      * @param object $oCategory The category.
  1338.      *
  1339.      * @return null
  1340.      */
  1341.     public function showCategoryEditForm($oCategory)
  1342.     {
  1343.         include UAM_REALPATH.'tpl/categoryEditForm.php';
  1344.     }
  1345.    
  1346.     /**
  1347.      * The function for the edit_category action.
  1348.      *
  1349.      * @param integer $iCategoryId The category _iId.
  1350.      *
  1351.      * @return null
  1352.      */
  1353.     public function saveCategoryData($iCategoryId)
  1354.     {
  1355.         $this->_saveObjectData('category', $iCategoryId);
  1356.     }
  1357.    
  1358.     /**
  1359.      * The function for the delete_category action.
  1360.      *
  1361.      * @param integer $iCategoryId The _iId of the category.
  1362.      *
  1363.      * @return null
  1364.      */
  1365.     public function removeCategoryData($iCategoryId)
  1366.     {
  1367.         /**
  1368.          * @var wpdb $wpdb
  1369.          */
  1370.         global $wpdb;
  1371.        
  1372.         $wpdb->query(
  1373.             "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
  1374.             WHERE object_id = ".$iCategoryId."
  1375.                 AND object_type = 'category'"
  1376.         );
  1377.     }
  1378.    
  1379.  
  1380.     /*
  1381.      * Functions for the pluggable object actions.
  1382.      */
  1383.    
  1384.     /**
  1385.      * The function for the pluggable save action.
  1386.      *
  1387.      * @param string  $sObjectType The name of the pluggable object.
  1388.      * @param integer $iObjectId   The pluggable object _iId.
  1389.      * @param array   $aUserGroups The user groups for the object.
  1390.      *
  1391.      * @return null
  1392.      */
  1393.     public function savePlObjectData($sObjectType, $iObjectId, $aUserGroups = null)
  1394.     {
  1395.         $this->_saveObjectData($sObjectType, $iObjectId, $aUserGroups);
  1396.     }
  1397.    
  1398.     /**
  1399.      * The function for the pluggable remove action.
  1400.      *
  1401.      * @param string  $sObjectName The name of the pluggable object.
  1402.      * @param integer $iObjectId   The pluggable object _iId.
  1403.      *
  1404.      * @return null
  1405.      */
  1406.     public function removePlObjectData($sObjectName, $iObjectId)
  1407.     {
  1408.         /**
  1409.          * @var wpdb $wpdb
  1410.          */
  1411.         global $wpdb;
  1412.  
  1413.         $wpdb->query(
  1414.             "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
  1415.             WHERE object_id = ".$iObjectId."
  1416.                AND object_type = ".$sObjectName
  1417.         );
  1418.     }
  1419.    
  1420.     /**
  1421.      * Returns the group selection form for pluggable _aObjects.
  1422.      *
  1423.      * @param string  $sObjectType     The object type.
  1424.      * @param integer $iObjectId       The _iId of the object.
  1425.      * @param string  $aGroupsFormName The name of the form which contains the groups.
  1426.      *
  1427.      * @return string;
  1428.      */
  1429.     public function showPlGroupSelectionForm($sObjectType, $iObjectId, $aGroupsFormName = null)
  1430.     {
  1431.         $sFileName = UAM_REALPATH.'tpl/groupSelectionForm.php';
  1432.         $aUamUserGroups = $this->getAccessHandler()->getUserGroups();
  1433.         $aUserGroupsForObject = $this->getAccessHandler()->getUserGroupsForObject($sObjectType, $iObjectId);
  1434.        
  1435.         if (is_file($sFileName)) {
  1436.             ob_start();
  1437.             include $sFileName;
  1438.             $sContents = ob_get_contents();
  1439.             ob_end_clean();
  1440.            
  1441.             return $sContents;
  1442.         }
  1443.        
  1444.         return '';
  1445.     }
  1446.    
  1447.     /**
  1448.      * Returns the column for a pluggable object.
  1449.      *
  1450.      * @param string  $sObjectType The object type.
  1451.      * @param integer $iObjectId   The object _iId.
  1452.      *
  1453.      * @return string
  1454.      */
  1455.     public function getPlColumn($sObjectType, $iObjectId)
  1456.     {
  1457.         return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iObjectId, $sObjectType);
  1458.     }
  1459.    
  1460.    
  1461.     /*
  1462.      * Functions for the blog content.
  1463.      */
  1464.    
  1465.     /**
  1466.      * Manipulates the wordpress query object to filter content.
  1467.      *
  1468.      * @param object $oWpQuery The wordpress query object.
  1469.      *
  1470.      * @return null
  1471.      */
  1472.     public function parseQuery($oWpQuery)
  1473.     {
  1474.         $aUamOptions = $this->getAdminOptions();
  1475.        
  1476.         if ($aUamOptions['hide_post'] == 'true') {
  1477.             $oUamAccessHandler = $this->getAccessHandler();
  1478.             $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
  1479.            
  1480.             if (count($aExcludedPosts) > 0) {
  1481.                 $oWpQuery->query_vars['post__not_in'] = array_merge(
  1482.                     $oWpQuery->query_vars['post__not_in'],
  1483.                     $aExcludedPosts
  1484.                 );
  1485.             }
  1486.         }
  1487.     }
  1488.    
  1489.     /**
  1490.      * Modifies the content of the post by the given settings.
  1491.      *
  1492.      * @param object $oPost The current post.
  1493.      *
  1494.      * @return object|null
  1495.      */
  1496.     protected function _getPost($oPost)
  1497.     {
  1498.         $aUamOptions = $this->getAdminOptions();
  1499.         $oUamAccessHandler = $this->getAccessHandler();
  1500.        
  1501.         $sPostType = $oPost->post_type;
  1502.  
  1503.         if ($this->getAccessHandler()->isPostableType($sPostType) && $sPostType != 'post' && $sPostType != 'page') {
  1504.             $sPostType = 'post';
  1505.         } elseif ($sPostType != 'post' && $sPostType != 'page') {
  1506.             return $oPost;
  1507.         }
  1508.        
  1509.         if ($aUamOptions['hide_'.$sPostType] == 'true' || $this->atAdminPanel()) {
  1510.             if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
  1511.                 $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
  1512.                 return $oPost;
  1513.             }
  1514.         } else {
  1515.             if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
  1516.                 $oPost->isLocked = true;
  1517.                
  1518.                 $sUamPostContent = $aUamOptions[$sPostType.'_content'];
  1519.                 $sUamPostContent = str_replace("[LOGIN_FORM]",  $this->getLoginBarHtml(), $sUamPostContent);
  1520.                
  1521.                 if ($aUamOptions['hide_'.$sPostType.'_title'] == 'true') {
  1522.                     $oPost->post_title = $aUamOptions[$sPostType.'_title'];
  1523.                 }
  1524.                
  1525.                 if ($aUamOptions[$sPostType.'_comments_locked'] == 'false') {
  1526.                     $oPost->comment_status = 'close';
  1527.                 }
  1528.  
  1529.                 if ($aUamOptions['show_post_content_before_more'] == 'true'
  1530.                     && $sPostType == "post"
  1531.                     && preg_match('/<!--more(.*?)?-->/', $oPost->post_content, $aMatches)
  1532.                 ) {
  1533.                     $oPost->post_content = explode($aMatches[0], $oPost->post_content, 2);
  1534.                     $sUamPostContent = $oPost->post_content[0] . " " . stripslashes($sUamPostContent);
  1535.                 }
  1536.  
  1537.                 $oPost->post_content = stripslashes($sUamPostContent);
  1538.             }
  1539.  
  1540.             $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
  1541.            
  1542.             return $oPost;
  1543.         }
  1544.        
  1545.         return null;
  1546.     }
  1547.    
  1548.     /**
  1549.      * The function for the the_posts filter.
  1550.      *
  1551.      * @param array $aPosts The posts.
  1552.      *
  1553.      * @return array
  1554.      */
  1555.     public function showPost($aPosts = array())
  1556.     {
  1557.         $aShowPosts = array();
  1558.         $aUamOptions = $this->getAdminOptions();
  1559.        
  1560.         if (!is_feed() || ($aUamOptions['protect_feed'] == 'true' && is_feed())) {
  1561.             foreach ($aPosts as $iPostId) {
  1562.                 if ($iPostId !== null) {
  1563.                     $oPost = $this->_getPost($iPostId);
  1564.  
  1565.                     if ($oPost !== null) {
  1566.                         $aShowPosts[] = $oPost;
  1567.                     }
  1568.                 }
  1569.             }
  1570.  
  1571.             $aPosts = $aShowPosts;
  1572.         }
  1573.        
  1574.         return $aPosts;
  1575.     }
  1576.    
  1577.     /**
  1578.      * The function for the posts_where_paged filter.
  1579.      *
  1580.      * @param string $sSql The where sql statement.
  1581.      *
  1582.      * @return string
  1583.      */
  1584.     public function showPostSql($sSql)
  1585.     {  
  1586.         $oUamAccessHandler = $this->getAccessHandler();
  1587.         $aUamOptions = $this->getAdminOptions();
  1588.        
  1589.         if ($aUamOptions['hide_post'] == 'true') {
  1590.             global $wpdb;
  1591.             $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
  1592.            
  1593.             if (count($aExcludedPosts) > 0) {
  1594.                 $sExcludedPostsStr = implode(",", $aExcludedPosts);
  1595.                 $sSql .= " AND $wpdb->posts.ID NOT IN($sExcludedPostsStr) ";
  1596.             }
  1597.         }
  1598.        
  1599.         return $sSql;
  1600.     }
  1601.    
  1602.     /**
  1603.      * The function for the wp_get_nav_menu_items filter.
  1604.      *
  1605.      * @param array $aItems The menu item.
  1606.      *
  1607.      * @return array
  1608.      */
  1609.     public function showCustomMenu($aItems)
  1610.     {
  1611.         $aShowItems = array();
  1612.        
  1613.         foreach ($aItems as $oItem) {
  1614.             if ($oItem->object == 'post' || $oItem->object == 'page') {
  1615.                 $oObject = $this->getPost($oItem->object_id);
  1616.              
  1617.                 if ($oObject !== null) {
  1618.                     $oPost = $this->_getPost($oObject);
  1619.  
  1620.                     if ($oPost !== null) {
  1621.                         if (isset($oPost->isLocked)) {
  1622.                             $oItem->title = $oPost->post_title;
  1623.                         }
  1624.  
  1625.                         $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
  1626.                         $aShowItems[] = $oItem;
  1627.                     }
  1628.                 }
  1629.             } elseif ($oItem->object == 'category') {
  1630.                 $oObject = $this->getCategory($oItem->object_id);
  1631.                 $oCategory = $this->_getTerm('category', $oObject);
  1632.  
  1633.                 if ($oCategory !== null && !$oCategory->isEmpty) {
  1634.                     $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
  1635.                     $aShowItems[] = $oItem;
  1636.                 }
  1637.             } else {
  1638.                 $aShowItems[] = $oItem;
  1639.             }
  1640.         }
  1641.        
  1642.         return $aShowItems;
  1643.     }
  1644.    
  1645.     /**
  1646.      * The function for the comments_array filter.
  1647.      *
  1648.      * @param array $aComments The comments.
  1649.      *
  1650.      * @return array
  1651.      */
  1652.     public function showComment($aComments = array())
  1653.     {
  1654.         $aShowComments = array();
  1655.         $aUamOptions = $this->getAdminOptions();
  1656.         $oUamAccessHandler = $this->getAccessHandler();
  1657.        
  1658.         foreach ($aComments as $oComment) {
  1659.             $oPost = $this->getPost($oComment->comment_post_ID);
  1660.             $sPostType = $oPost->post_type;
  1661.            
  1662.             if ($aUamOptions['hide_'.$sPostType.'_comment'] == 'true'
  1663.                 || $aUamOptions['hide_'.$sPostType] == 'true'
  1664.                 || $this->atAdminPanel()
  1665.             ) {
  1666.                 if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
  1667.                     $aShowComments[] = $oComment;
  1668.                 }
  1669.             } else {
  1670.                 if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
  1671.                     $oComment->comment_content = $aUamOptions[$sPostType.'_comment_content'];
  1672.                 }
  1673.                
  1674.                 $aShowComments[] = $oComment;
  1675.             }
  1676.         }
  1677.        
  1678.         $aComments = $aShowComments;
  1679.        
  1680.         return $aComments;
  1681.     }
  1682.    
  1683.     /**
  1684.      * The function for the get_pages filter.
  1685.      *
  1686.      * @param array $aPages The pages.
  1687.      *
  1688.      * @return array
  1689.      */
  1690.     public function showPage($aPages = array())
  1691.     {
  1692.         $aShowPages = array();
  1693.         $aUamOptions = $this->getAdminOptions();
  1694.         $oUamAccessHandler = $this->getAccessHandler();
  1695.        
  1696.         foreach ($aPages as $oPage) {
  1697.             if ($aUamOptions['hide_page'] == 'true'
  1698.                 || $this->atAdminPanel()
  1699.             ) {
  1700.                 if ($oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
  1701.                     $oPage->post_title .= $this->adminOutput(
  1702.                         $oPage->post_type,
  1703.                         $oPage->ID
  1704.                     );
  1705.                     $aShowPages[] = $oPage;
  1706.                 }
  1707.             } else {
  1708.                 if (!$oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
  1709.                     if ($aUamOptions['hide_page_title'] == 'true') {
  1710.                         $oPage->post_title = $aUamOptions['page_title'];
  1711.                     }
  1712.  
  1713.                     $oPage->post_content = $aUamOptions['page_content'];
  1714.                 }
  1715.  
  1716.                 $oPage->post_title .= $this->adminOutput($oPage->post_type, $oPage->ID);
  1717.                 $aShowPages[] = $oPage;
  1718.             }
  1719.         }
  1720.        
  1721.         $aPages = $aShowPages;
  1722.        
  1723.         return $aPages;
  1724.     }
  1725.    
  1726.     /**
  1727.      * Modifies the content of the term by the given settings.
  1728.      *
  1729.      * @param string $sTermType The type of the term.
  1730.      * @param object $oTerm     The current term.
  1731.      *
  1732.      * @return object|null
  1733.      */
  1734.     protected function _getTerm($sTermType, $oTerm)
  1735.     {
  1736.         $aUamOptions = $this->getAdminOptions();
  1737.         $oUamAccessHandler = $this->getAccessHandler();
  1738.        
  1739.         $oTerm->isEmpty = false;
  1740.        
  1741.         $oTerm->name .= $this->adminOutput('term', $oTerm->term_id);
  1742.        
  1743.         if ($sTermType == 'post_tag'
  1744.             || $sTermType == 'category'
  1745.             && $oUamAccessHandler->checkObjectAccess('category', $oTerm->term_id)
  1746.         ) {
  1747.             if ($this->atAdminPanel() == false
  1748.                 && ($aUamOptions['hide_post'] == 'true'
  1749.                 || $aUamOptions['hide_page'] == 'true')
  1750.             ) {
  1751.                 $iTermRequest = $oTerm->term_id;
  1752.                 $sTermRequestType = $sTermType;
  1753.                
  1754.                 if ($sTermType == 'post_tag') {
  1755.                     $iTermRequest = $oTerm->slug;
  1756.                     $sTermRequestType = 'tag';
  1757.                 }
  1758.                
  1759.                 $aArgs = array(
  1760.                     'numberposts' => - 1,
  1761.                     $sTermRequestType => $iTermRequest
  1762.                 );
  1763.                
  1764.                 $aTermPosts = get_posts($aArgs);
  1765.                 $oTerm->count = count($aTermPosts);
  1766.                
  1767.                 if (isset($aTermPosts)) {
  1768.                     foreach ($aTermPosts as $oPost) {
  1769.                         if ($aUamOptions['hide_'.$oPost->post_type] == 'true'
  1770.                             && !$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)
  1771.                         ) {
  1772.                             $oTerm->count--;
  1773.                         }
  1774.                     }
  1775.                 }
  1776.                
  1777.                 //For post_tags
  1778.                 if ($sTermType == 'post_tag' && $oTerm->count <= 0) {
  1779.                     return null;
  1780.                 }
  1781.                
  1782.                 //For categories
  1783.                 if ($oTerm->count <= 0
  1784.                     && $aUamOptions['hide_empty_categories'] == 'true'
  1785.                     && ($oTerm->taxonomy == "term"
  1786.                     || $oTerm->taxonomy == "category")
  1787.                 ) {
  1788.                     $oTerm->isEmpty = true;
  1789.                 }
  1790.                
  1791.                 if ($aUamOptions['lock_recursive'] == 'false') {
  1792.                     $oCurCategory = $oTerm;
  1793.                    
  1794.                     while ($oCurCategory->parent != 0) {
  1795.                         $oCurCategory = get_term($oCurCategory->parent, 'category');
  1796.                        
  1797.                         if ($oUamAccessHandler->checkObjectAccess('term', $oCurCategory->term_id)) {
  1798.                             $oTerm->parent = $oCurCategory->term_id;
  1799.                             break;
  1800.                         }
  1801.                     }
  1802.                 }
  1803.             }
  1804.  
  1805.             return $oTerm;
  1806.         }
  1807.        
  1808.         return null;
  1809.     }
  1810.    
  1811.     /**
  1812.      * The function for the get_terms filter.
  1813.      *
  1814.      * @param array $aTerms The terms.
  1815.      * @param array $aArgs  The given arguments.
  1816.      *
  1817.      * @return array
  1818.      */
  1819.     public function showTerms($aTerms = array(), $aArgs = array())
  1820.     {
  1821.         $aShowTerms = array();
  1822.  
  1823.         foreach ($aTerms as $oTerm) {
  1824.             if (!is_object($oTerm)) {
  1825.                 return $aTerms;
  1826.             }
  1827.  
  1828.             if ($oTerm->taxonomy == 'category'  || $oTerm->taxonomy == 'post_tag') {
  1829.                 $oTerm = $this->_getTerm($oTerm->taxonomy, $oTerm);
  1830.             }
  1831.  
  1832.             if ($oTerm !== null && (!isset($oTerm->isEmpty) || !$oTerm->isEmpty)) {
  1833.                 $aShowTerms[$oTerm->term_id] = $oTerm;
  1834.             }
  1835.         }
  1836.        
  1837.         foreach ($aTerms as $sKey => $oTerm) {
  1838.             if (!isset($aShowTerms[$oTerm->term_id])) {
  1839.                 unset($aTerms[$sKey]);
  1840.             }
  1841.         }
  1842.        
  1843.         return $aTerms;
  1844.     }
  1845.    
  1846.     /**
  1847.      * The function for the get_previous_post_where and
  1848.      * the get_next_post_where filter.
  1849.      *
  1850.      * @param string $sSql The current sql string.
  1851.      *
  1852.      * @return string
  1853.      */
  1854.     public function showNextPreviousPost($sSql)
  1855.     {
  1856.         $oUamAccessHandler = $this->getAccessHandler();
  1857.         $aUamOptions = $this->getAdminOptions();
  1858.        
  1859.         if ($aUamOptions['hide_post'] == 'true') {
  1860.             $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
  1861.            
  1862.             if (count($aExcludedPosts) > 0) {
  1863.                 $sExcludedPosts = implode(",", $aExcludedPosts);
  1864.                 $sSql.= " AND p.ID NOT IN($sExcludedPosts) ";
  1865.             }
  1866.         }
  1867.        
  1868.         return $sSql;
  1869.     }
  1870.      
  1871.     /**
  1872.      * Returns the admin hint.
  1873.      *
  1874.      * @param string  $sObjectType The object type.
  1875.      * @param integer $iObjectId   The object _iId we want to check.
  1876.      *
  1877.      * @return string
  1878.      */
  1879.     public function adminOutput($sObjectType, $iObjectId)
  1880.     {
  1881.         $sOutput = "";
  1882.        
  1883.         if (!$this->atAdminPanel()) {
  1884.             $aUamOptions = $this->getAdminOptions();
  1885.            
  1886.             if ($aUamOptions['blog_admin_hint'] == 'true') {
  1887.                 $oCurrentUser = $this->getCurrentUser();
  1888.  
  1889.                 $oUserData = get_userdata($oCurrentUser->ID);
  1890.  
  1891.                 if (!isset($oUserData->user_level)) {
  1892.                     return $sOutput;
  1893.                 }
  1894.  
  1895.                 $oUamAccessHandler = $this->getAccessHandler();
  1896.  
  1897.                 if ($oUamAccessHandler->userIsAdmin($oCurrentUser->ID)
  1898.                     && count($oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId)) > 0
  1899.                 ) {
  1900.                     $sOutput .= $aUamOptions['blog_admin_hint_text'];
  1901.                 }
  1902.             }
  1903.         }
  1904.        
  1905.         return $sOutput;
  1906.     }
  1907.    
  1908.     /**
  1909.      * The function for the edit_post_link filter.
  1910.      *
  1911.      * @param string  $sLink   The edit link.
  1912.      * @param integer $iPostId The _iId of the post.
  1913.      *
  1914.      * @return string
  1915.      */
  1916.     public function showGroupMembership($sLink, $iPostId)
  1917.     {
  1918.         $oUamAccessHandler = $this->getAccessHandler();
  1919.         $aGroups = $oUamAccessHandler->getUserGroupsForObject('post', $iPostId);
  1920.        
  1921.         if (count($aGroups) > 0) {
  1922.             $sLink .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
  1923.            
  1924.             foreach ($aGroups as $oGroup) {
  1925.                 $sLink .= $oGroup->getGroupName().', ';
  1926.             }
  1927.            
  1928.             $sLink = rtrim($sLink, ', ');
  1929.         }
  1930.        
  1931.         return $sLink;
  1932.     }
  1933.    
  1934.     /**
  1935.      * Returns the login bar.
  1936.      *
  1937.      * @return string
  1938.      */
  1939.     public function getLoginBarHtml()
  1940.     {
  1941.         if (!is_user_logged_in()) {
  1942.             return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
  1943.         }
  1944.        
  1945.         return '';
  1946.     }
  1947.  
  1948.    
  1949.     /*
  1950.      * Functions for the redirection and files.
  1951.      */
  1952.    
  1953.     /**
  1954.      * Returns true if permalinks are active otherwise false.
  1955.      *
  1956.      * @return boolean
  1957.      */
  1958.     public function isPermalinksActive()
  1959.     {
  1960.         $sPermalinkStructure = $this->getWpOption('permalink_structure');
  1961.            
  1962.         if (empty($sPermalinkStructure)) {
  1963.             return false;
  1964.         } else {
  1965.             return true;
  1966.         }
  1967.     }
  1968.    
  1969.     /**
  1970.      * Redirects to a page or to content.
  1971.      *  
  1972.      * @param string $sHeaders    The headers which are given from wordpress.
  1973.      * @param object $oPageParams The params of the current page.
  1974.      *
  1975.      * @return string
  1976.      */
  1977.     public function redirect($sHeaders, $oPageParams)
  1978.     {
  1979.         $oUamOptions = $this->getAdminOptions();
  1980.        
  1981.         if (isset($_GET['uamgetfile']) && isset($_GET['uamfiletype'])) {
  1982.             $sFileUrl = $_GET['uamgetfile'];
  1983.             $sFileType = $_GET['uamfiletype'];
  1984.             $this->getFile($sFileType, $sFileUrl);
  1985.         } elseif (!$this->atAdminPanel() && $oUamOptions['redirect'] != 'false') {
  1986.             $oObject = null;
  1987.        
  1988.             if (isset($oPageParams->query_vars['p'])) {
  1989.                 $oObject = $this->getPost($oPageParams->query_vars['p']);
  1990.                 $oObjectType = $oObject->post_type;
  1991.                 $iObjectId = $oObject->ID;
  1992.             } elseif (isset($oPageParams->query_vars['page_id'])) {
  1993.                 $oObject = $this->getPost($oPageParams->query_vars['page_id']);
  1994.                 $oObjectType = $oObject->post_type;
  1995.                 $iObjectId = $oObject->ID;
  1996.             } elseif (isset($oPageParams->query_vars['cat_id'])) {
  1997.                 $oObject = $this->getCategory($oPageParams->query_vars['cat_id']);
  1998.                 $oObjectType = 'category';
  1999.                 $iObjectId = $oObject->term_id;
  2000.             } elseif (isset($oPageParams->query_vars['name'])) {
  2001.                 $oObject = get_page_by_title($oPageParams->query_vars['name'], OBJECT, 'post');
  2002.  
  2003.                 if ($oObject !== null) {
  2004.                     $oObjectType = $oObject->post_type;
  2005.                     $iObjectId = $oObject->ID;
  2006.                 }
  2007.             } elseif (isset($oPageParams->query_vars['pagename'])) {
  2008.                 $oObject = get_page_by_title($oPageParams->query_vars['pagename']);
  2009.  
  2010.                 if ($oObject !== null) {
  2011.                     $oObjectType = $oObject->post_type;
  2012.                     $iObjectId = $oObject->ID;
  2013.                 }
  2014.             }
  2015.            
  2016.             if ($oObject === null || $oObject !== null && isset($oObjectType) && isset($iObjectId)
  2017.                 && !$this->getAccessHandler()->checkObjectAccess($oObjectType, $iObjectId)
  2018.             ) {
  2019.                 $this->redirectUser($oObject);
  2020.             }
  2021.         }
  2022.  
  2023.         return $sHeaders;
  2024.     }
  2025.    
  2026.     /**
  2027.      * Returns the current url.
  2028.      *
  2029.      * @return string
  2030.      */
  2031.     public function getCurrentUrl()
  2032.     {
  2033.         if (!isset($_SERVER['REQUEST_URI'])) {
  2034.             $sServerRequestUri = $_SERVER['PHP_SELF'];
  2035.         } else {
  2036.             $sServerRequestUri = $_SERVER['REQUEST_URI'];
  2037.         }
  2038.        
  2039.         $sSecure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  2040.         $aProtocols = explode("/", strtolower($_SERVER["SERVER_PROTOCOL"]));
  2041.         $sProtocol = $aProtocols[0].$sSecure;
  2042.         $sPort = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
  2043.        
  2044.         return $sProtocol."://".$_SERVER['SERVER_NAME'].$sPort.$sServerRequestUri;
  2045.     }
  2046.    
  2047.     /**
  2048.      * Redirects the user to his destination.
  2049.      *
  2050.      * @param object $oObject The current object we want to access.
  2051.      *
  2052.      * @return null
  2053.      */
  2054.     public function redirectUser($oObject = null)
  2055.     {
  2056.         global $wp_query;
  2057.        
  2058.         $blPostToShow = false;
  2059.         $aPosts = $wp_query->get_posts();
  2060.        
  2061.         if ($oObject === null && isset($aPosts)) {
  2062.             foreach ($aPosts as $oPost) {
  2063.                 if ($this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID)) {
  2064.                     $blPostToShow = true;
  2065.                     break;
  2066.                 }
  2067.             }
  2068.         }
  2069.        
  2070.         if (!$blPostToShow) {
  2071.             $aUamOptions = $this->getAdminOptions();
  2072.  
  2073.             if ($aUamOptions['redirect'] == 'custom_page') {
  2074.                 $oPost = $this->getPost($aUamOptions['redirect_custom_page']);
  2075.                 $sUrl = $oPost->guid;
  2076.             } elseif ($aUamOptions['redirect'] == 'custom_url') {
  2077.                 $sUrl = $aUamOptions['redirect_custom_url'];
  2078.             } else {
  2079.                 $sUrl = home_url('/');
  2080.             }
  2081.  
  2082.             if ($sUrl != $this->getCurrentUrl()) {
  2083.                 wp_redirect($sUrl);
  2084.                 exit;
  2085.             }      
  2086.         }
  2087.     }
  2088.    
  2089.     /**
  2090.      * Delivers the content of the requested file.
  2091.      *
  2092.      * @param string $sObjectType The type of the requested file.
  2093.      * @param string $sObjectUrl  The file url.
  2094.      *
  2095.      * @return null
  2096.      */
  2097.     public function getFile($sObjectType, $sObjectUrl)
  2098.     {
  2099.         $oObject = $this->_getFileSettingsByType($sObjectType, $sObjectUrl);
  2100.  
  2101.         if ($oObject === null) {
  2102.             return null;
  2103.         }
  2104.        
  2105.         $sFile = null;
  2106.        
  2107.         if ($this->getAccessHandler()->checkObjectAccess($oObject->type, $oObject->id)) {
  2108.             $sFile = $oObject->file;
  2109.         } elseif ($oObject->isImage) {
  2110.             $sFile = UAM_REALPATH.'gfx/noAccessPic.png';
  2111.         } else {
  2112.             wp_die(TXT_UAM_NO_RIGHTS);
  2113.         }
  2114.        
  2115.         //Deliver content
  2116.         if (file_exists($sFile)) {
  2117.             $sFileName = basename($sFile);
  2118.            
  2119.             /*
  2120.              * This only for compatibility
  2121.              * mime_content_type has been deprecated as the PECL extension file info
  2122.              * provides the same functionality (and more) in a much cleaner way.
  2123.              */
  2124.             $sFileExt = strtolower(array_pop(explode('.', $sFileName)));
  2125.             $aMimeTypes = $this->_getMimeTypes();
  2126.  
  2127.             if (function_exists('finfo_open')) {
  2128.                 $sFileInfo = finfo_open(FILEINFO_MIME);
  2129.                 $sFileMimeType = finfo_file($sFileInfo, $sFile);
  2130.                 finfo_close($sFileInfo);
  2131.             } elseif (function_exists('mime_content_type')) {
  2132.                 $sFileMimeType = mime_content_type($sFile);
  2133.             } elseif (isset($aMimeTypes[$sFileExt])) {
  2134.                 $sFileMimeType = $aMimeTypes[$sFileExt];
  2135.             } else {
  2136.                 $sFileMimeType = 'application/octet-stream';
  2137.             }
  2138.            
  2139.             header('Content-Description: File Transfer');
  2140.             header('Content-Type: '.$sFileMimeType);
  2141.            
  2142.             if (!$oObject->isImage) {
  2143.                 $sBaseName = str_replace(' ', '_', basename($sFile));
  2144.                 header('Content-Disposition: attachment; filename="'.$sBaseName.'"');
  2145.             }
  2146.            
  2147.             header('Content-Transfer-Encoding: binary');
  2148.             header('Content-Length: '.filesize($sFile));
  2149.  
  2150.             $aUamOptions = $this->getAdminOptions();
  2151.            
  2152.             if ($aUamOptions['download_type'] == 'fopen'
  2153.                 && !$oObject->isImage
  2154.             ) {
  2155.                 $oHandler = fopen($sFile, 'r');
  2156.                
  2157.                 //TODO find better solution (prevent '\n' / '0A')
  2158.                 ob_clean();
  2159.                 flush();
  2160.                
  2161.                 while (!feof($oHandler)) {
  2162.                     if (!ini_get('safe_mode')) {
  2163.                         set_time_limit(30);
  2164.                     }
  2165.  
  2166.                     echo fread($oHandler, 1024);
  2167.                 }
  2168.                                
  2169.                 exit;
  2170.             } else {
  2171.                 ob_clean();
  2172.                 flush();
  2173.                 readfile($sFile);
  2174.                 exit;
  2175.             }
  2176.         } else {
  2177.             wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
  2178.         }
  2179.     }
  2180.    
  2181.     /**
  2182.      * Returns the file object by the given type and url.
  2183.      *
  2184.      * @param string $sObjectType The type of the requested file.
  2185.      * @param string $sObjectUrl  The file url.
  2186.      *
  2187.      * @return object|null
  2188.      */
  2189.     protected function _getFileSettingsByType($sObjectType, $sObjectUrl)
  2190.     {
  2191.         $oObject = null;
  2192.  
  2193.         if ($sObjectType == 'attachment') {
  2194.             $aUploadDir = wp_upload_dir();
  2195.  
  2196.             $sMultiPath = str_replace(ABSPATH, '/', $aUploadDir['basedir']);
  2197.             $sMultiPath = str_replace('/files', $sMultiPath, $aUploadDir['baseurl']);
  2198.            
  2199.             if ($this->isPermalinksActive()) {
  2200.                 $sObjectUrl = $sMultiPath.'/'.$sObjectUrl;
  2201.             }
  2202.            
  2203.             $oPost = $this->getPost($this->getPostIdByUrl($sObjectUrl));
  2204.    
  2205.             if ($oPost !== null
  2206.                 && $oPost->post_type == 'attachment'
  2207.             ) {
  2208.                 $oObject = new stdClass();
  2209.                 $oObject->id = $oPost->ID;
  2210.                 $oObject->isImage = wp_attachment_is_image($oPost->ID);
  2211.                 $oObject->type = $sObjectType;
  2212.                 $oObject->file = $aUploadDir['basedir'].str_replace($sMultiPath, '', $sObjectUrl );
  2213.             }
  2214.         } else {
  2215.             $aPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
  2216.            
  2217.             if (isset($aPlObject) && isset($aPlObject['getFileObject'])) {
  2218.                 $oObject = $aPlObject['reference']->{$aPlObject['getFileObject']}($sObjectUrl);
  2219.             }
  2220.         }
  2221.        
  2222.         return $oObject;
  2223.     }
  2224.    
  2225.     /**
  2226.      * Returns the url for a locked file.
  2227.      *
  2228.      * @param string  $sUrl The base url.
  2229.      * @param integer $iId  The _iId of the file.
  2230.      *
  2231.      * @return string
  2232.      */
  2233.     public function getFileUrl($sUrl, $iId)
  2234.     {
  2235.         $aUamOptions = $this->getAdminOptions();
  2236.            
  2237.         if (!$this->isPermalinksActive() && $aUamOptions['lock_file'] == 'true') {
  2238.             $oPost = &$this->getPost($iId);
  2239.             $aType = explode("/", $oPost->post_mime_type);
  2240.             $sType = $aType[1];
  2241.             $aFileTypes = explode(',', $aUamOptions['locked_file_types']);
  2242.            
  2243.             if ($aUamOptions['lock_file_types'] == 'all' || in_array($sType, $aFileTypes)) {
  2244.                 $sUrl = home_url('/').'?uamfiletype=attachment&uamgetfile='.$sUrl;
  2245.             }
  2246.         }
  2247.        
  2248.         return $sUrl;
  2249.     }
  2250.    
  2251.     /**
  2252.      * Returns the post by the given url.
  2253.      *
  2254.      * @param string $sUrl The url of the post(attachment).
  2255.      *
  2256.      * @return object The post.
  2257.      */
  2258.     public function getPostIdByUrl($sUrl)
  2259.     {
  2260.         if (isset($this->_aPostUrls[$sUrl])) {
  2261.             return $this->_aPostUrls[$sUrl];
  2262.         }
  2263.        
  2264.         $this->_aPostUrls[$sUrl] = null;
  2265.        
  2266.         //Filter edit string
  2267.         $sNewUrl = preg_split("/-e[0-9]{1,}/", $sUrl);
  2268.  
  2269.         if (count($sNewUrl) == 2) {
  2270.             $sNewUrl = $sNewUrl[0].$sNewUrl[1];
  2271.         } else {
  2272.             $sNewUrl = $sNewUrl[0];
  2273.         }
  2274.        
  2275.         //Filter size
  2276.         $sNewUrl = preg_split("/-[0-9]{1,}x[0-9]{1,}/", $sNewUrl);
  2277.  
  2278.         if (count($sNewUrl) == 2) {
  2279.             $sNewUrl = $sNewUrl[0].$sNewUrl[1];
  2280.         } else {
  2281.             $sNewUrl = $sNewUrl[0];
  2282.         }
  2283.  
  2284.         /**
  2285.          * @var wpdb $wpdb
  2286.          */
  2287.         global $wpdb;
  2288.  
  2289.         $oDbPost = $wpdb->get_row(
  2290.             "SELECT ID
  2291.             FROM ".$wpdb->prefix."posts
  2292.             WHERE guid = '" . $sNewUrl . "'
  2293.             LIMIT 1"
  2294.         );
  2295.        
  2296.         if ($oDbPost) {
  2297.             $this->_aPostUrls[$sUrl] = $oDbPost->ID;
  2298.         }
  2299.        
  2300.         return $this->_aPostUrls[$sUrl];
  2301.     }
  2302.    
  2303.     /**
  2304.      * Caches the urls for the post for a later lookup.
  2305.      *
  2306.      * @param string $sUrl  The url of the post.
  2307.      * @param object $oPost The post object.
  2308.      *
  2309.      * @return null
  2310.      */
  2311.     public function cachePostLinks($sUrl, $oPost)
  2312.     {
  2313.         $this->_aPostUrls[$sUrl] = $oPost->ID;
  2314.         return $sUrl;
  2315.     }
  2316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement