Previous page';
/**
* Next page link
* @var string
*/
var $next_page_link = 'Next page';
/**
* Conditions that indicate when a user should be ignored
* @var array
*/
var $always_ignore = null;
/**
* The list of available AD fields from which the user can choose
* @var array
*/
protected $_available_fields = array();
/**
* The output template for an individual item in the list
* @var string
*/
var $output_builder = ' [if mail] [elseif targetaddress] [endif]
';
/**
* The default size of gravatar to retrieve
* @var int
*/
var $gravatar_size = 100;
/**
* Build our object
*/
function __construct() {
$this->basepath = str_replace( array( basename( __FILE__ ), basename( dirname( __FILE__ ) ) ), '', realpath( __FILE__ ) );
/*$this->_set_transient_timeout( 60*60 );*/
add_action( 'init', array( &$this, '_init' ) );
/**
* Move this call out of __construct() to avoid deprecated notices
*/
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_and_styles' ) );
}
/**
* Register our scripts and styles for this plugin
*/
function enqueue_scripts_and_styles() {
wp_register_script( 'ad-employee-list-admin', plugins_url( 'js/active-directory-employee-list.admin.js', dirname( __FILE__ ) ), array( 'jquery', 'post' ), '0.3', true );
wp_register_style( 'ad-employee-list-admin-style', plugins_url( 'css/active-directory-employee-list.admin.css', dirname( __FILE__ ) ), array( 'widgets' ), '0.3', 'all' );
}
/**
* Perform any actions that need to happen upon WordPress init
*/
function _init() {
if( !class_exists( 'adLDAPE' ) )
require_once( $this->basepath . '/inc/adLDAP-extended.php' );
}
function register_widget() {
register_widget( 'ADEL_List_Widget' );
register_widget( 'ADEL_Single_Widget' );
return register_widget( 'active_directory_employee_list_widget' );
}
/**
* Check for the existence of an action before adding it
*/
function add_action( $tag, $callback ) {
if( !has_action( $tag, $callback ) )
add_action( $tag, $callback );
}
/**
* Check for the existence of a filter before adding it
*/
function add_filter( $tag, $callback ) {
if( !has_filter( $tag, $callback ) )
add_filter( $tag, $callback );
}
/**
* Add information/output to the error log
*/
protected function _log() {
if( !$this->_print_debug )
return;
error_log( "\n\n" );
$args = func_get_args();
foreach( $args as $arg ) {
if( is_string( $arg ) )
error_log( $arg );
else
error_log( print_r( $arg, true ) );
}
error_log( "\n\n" );
}
/**
* Set the _transient_timeout property of this object
* Determine how long transients should be stored in the database
*/
protected function _set_transient_timeout( $t=60 ) {
$this->_transient_timeout = $t;
}
/**
* Retrieve the settings and preferences for this plugin.
* Recursively retrieves options from the current site, then
* network, then multi-network. If none exist, false is
* returned.
* Sets each appropriate object property with the value retrieved
* from the database.
* @return bool|array Returns false if no options are retrieved, returns
* an array of the retrieved options if they do
*
* @uses active_directory_employee_list::get_option()
* @uses active_directory_employee_list::_format_options()
* @uses maybe_unserialize()
*/
function _get_options() {
$g_opt = $this->get_option( $this->settings_name, false );
$opt = $this->get_option( $this->prefs_name, false );
$o_opt = $this->get_option( $this->output_name, false );
if( !is_array( $g_opt ) )
$g_opt = array();
if( !is_array( $opt ) )
$opt = array();
if( !is_array( $o_opt ) )
$o_opt = array();
$opt = array_merge( $opt, $g_opt, $o_opt );
if( empty( $opt ) )
$opt = false;
if( is_array( $opt ) ) {
$opt = $this->_format_options( $opt );
foreach( $opt as $k=>$v ) {
if( property_exists( $this, $k ) ) {
$this->$k = maybe_unserialize( $v );
}
}
}
if( !empty( $this->ad_group ) && !is_array( $this->ad_group ) )
$this->ad_group = array_map( 'trim', explode( ';', $this->ad_group ) );
if( isset( $_GET['ad_group'] ) )
$this->ad_group = $_GET['ad_group'];
return $opt;
}
/**
* Retrieve a set of options from the database
* If the options exist in the options table, those are retrieved. If not, the options
* are retrieved from the sitemeta table. If the options don't exist there, either,
* and the get_mnetwork_option function exists, an attempt is made to retrieve them
* from there.
*
* @param string $optname the name of the option to be retrieved
* @param mixed the default value to return is no options are retrieved
* @return mixed either the retrieved option or the default param
*
* @uses get_option() to retrieve options from the options table
* @uses get_site_option() to retrieve options from the sitemeta table
* @uses get_mnetwork_option() to retrieve options from the mnetwork_meta table (if exists)
* @uses maybe_unserialize() to possibly unserialize the returned result (though, that
* should occur within each of the individual functions to get the options)
*/
protected function get_option( $optname, $default=false ) {
if( $default === ( $opt = get_option( $optname, $default ) ) || empty( $opt ) ) {
/*$this->_log( "\n\n" );*/
if( $default === ( $opt = get_site_option( $optname, $default ) ) || empty( $opt ) ) {
/*$this->_log( "\n\n" );*/
if( function_exists( 'get_mnetwork_option' ) ) {
$opt = get_mnetwork_option( $optname, $default );
/*$this->_log( "\n\n" );*/
} else {
/*$this->_log( "\n\n" );*/
}
} else {
/*$this->_log( "\n\n" );*/
}
} else {
/*$this->_log( "\n\n" );*/
}
$opt = stripslashes_deep( maybe_unserialize( $opt ) );
/*$this->_log( "\n\n" );*/
return $opt;
}
/**
* Formats any options after being retrieved from the database for use by the plugin
*/
protected function _format_options( $opts ) {
if( is_array( $opts ) && array_key_exists( '_ad_password', $opts ) )
$opts['_ad_password'] = base64_decode( $opts['_ad_password'] );
return $opts;
}
/**
* Instantiate our adLDAPE object and perform initial bind
* @uses do_action() to run the adel_set_ldap_options action to set any global LDAP options after connect
*/
function open_ldap() {
if( is_object( $this->ldap ) )
return true;
/*$this->_log( "\n\n" );*/
try {
$this->ldap = new adLDAPE( array(
'base_dn' => $this->_base_dn,
'domain_controllers' => $this->_domain_controllers,
'ad_username' => $this->_ad_username,
'ad_password' => $this->_ad_password,
'use_ssl' => $this->_use_ssl,
'use_tls' => $this->_use_tls,
'account_suffix' => $this->_account_suffix,
) );
if ( is_object( $this->ldap ) )
do_action( 'adel_set_ldap_options', $this->ldap );
} catch( Exception $e ) {
return $e->getMessage();
}
return true;
}
/**
* Retrieve a list of all available AD groups
*/
function get_all_groups() {
$transname = 'adel_available_groups';
if( 0 === $this->transient_timeout ) {
delete_site_transient( $transname );
delete_transient( $transname );
}
if( is_network_admin() && ( false !== ( $g = get_site_transient( $transname ) ) ) )
return $g;
elseif( is_admin() && ( false !== ( $g = get_transient( $transname ) ) ) )
return $g;
if( true === $this->open_ldap() ) {
try{
$g = $this->ldap->search_groups( null, true, '*', true, 'cn', 'description' );
} catch( Exception $e ) {
$g = null;
}
} else {
return null;
}
if( is_network_admin() ) {
delete_site_transient( $transname );
set_site_transient( $transname, $g, $this->transient_timeout );
} elseif( is_admin() ) {
delete_transient( $transname );
set_transient( $transname, $g, $this->transient_timeout );
}
return $g;
}
/**
* Get a list of the allowed template tags
*/
function get_template_tags( $keys=true ) {
if( isset( $this->_available_fields ) )
return $keys ? array_keys( $this->_available_fields ) : $this->_available_fields;
/**
* Descriptions/list of AD/LDAP fields gleened from
* http://www.computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm
*/
$tags = array(
'cn' => 'Common name - First name and last name together',
'description' => 'Full text description of user/group',
'displayname' => 'The name that should be displayed as the user\'s name',
'dn' => 'The pre-formatted user string used to bind to active directory',
'givenname' => 'The user\'s first name',
'name' => 'Should be the same as CN',
'samaccountname' => 'The unique user ID of the user (generally the login name)',
'sn' => 'The user\'s last name',
'userprincipalname' => 'A unique user ID, complete with domain, used for logging in',
'mail' => 'The user\'s email address',
'mailnickname' => 'The username portion of the user\'s email address',
'c' => 'Country or region',
'company' => 'The name of the user\'s company',
'department' => 'The name of the user\'s department in the company',
'homephone' => 'The user\'s home telephone number',
'l' => 'The physical location (city) of the user',
'location' => 'The computer location (??) of the user?',
'manager' => 'The user\'s boss or manager',
'mobile' => 'The user\'s mobile phone number',
'ou' => 'Organizational unit',
'postalcode' => 'ZIP code',
'st' => 'State, province or county',
'streetaddress' => 'First line of postal address',
'targetaddress' => 'Alternate email address',
'telephonenumber' => 'Office phone number',
'gravatar' => 'The URL to the gravatar image for the user (not AD)',
);
return $keys ? array_keys( $tags ) : $tags;
}
/**
* Retrieve some instructions for using the output builder
* @return string some HTML code explaining how to use the output builder
*
* @uses active_directory_employee_list::get_template_tags()
*/
function get_output_builder_instructions() {
$ob_note = $this->get_template_tags();
foreach( $ob_note as $k=>$o ) {
$ob_note[$k] = '%' . $o . '%';
}
$ob_note = sprintf( __( '
You can use any of the following tags (assuming you have set the plugin to retrieve these fields) within the output builder:
%s', $this->text_domain ), '
' . implode( '
', $ob_note ) . '
' );
$ob_note .= sprintf( __( '
You can also use conditional statements within the output builder. The conditional statements should begin with an [if] block and end with an [endif] block. They can also include [elseif] and [else] blocks. The [if] and [elseif] blocks should include a condition.
For instance, if you would like check to see if the user has an email address set in their active directory profile, you would use [if mail] or [elseif mail].
The conditional statements simply check for the existence of a value in the field provided. You cannot nest conditional statements, nor can you provide multiple conditions in a single [if]/[elseif] (not yet, at least), unfortunately.
An example of a conditional might look something like:
%s
', $this->text_domain ), '<h3>[if mail]<a href="mailto:%mail%">%givenname% %sn%</a>[elseif displayname]%displayname%[else]%givenname% %sn%[endif]</h3>' );
return $ob_note;
}
/**
* Sort a multi-dimensional array by a value in the nested arrays
* @param array &$array the array to be sorted
* @param string $sort_field the key to be used as the sort field
* @param string $order in which direction to sort the array ('asc' or 'desc')
* @return array the sorted array
*/
function _sort_by_val( &$array, $sort_field, $order='asc' ) {
/*error_log( '[ADEL Notice]: Preparing to sort the array of users by ' . $sort_field . ' in ' . $order . ' order' );*/
$tmp = array();
foreach( $array as $key=>$sub ) {
$tmp[$key] = array_key_exists( $sort_field, $sub ) ? $sub[$sort_field] : null;
}
if( 'desc' === $order )
arsort( $tmp, SORT_STRING );
else
asort( $tmp, SORT_STRING );
$keys = array_keys( $tmp );
$tmp = $array;
$array = array();
foreach( $keys as $k ) {
$array[$k] = $tmp[$k];
}
}
/**
* Build a couple of simple presets for the output builder
* @return array an array of the simple presets
*/
function _get_output_presets() {
$parts = array();
$parts['name'] = ' [if givenname && sn]%givenname% %middlename% %sn%
[elseif cn]%cn%
[else]%displayname%[/endif]';
$parts['email'] = ' [if targetaddress]