_className = 'redactorModel'; //sets all of the plugin urls so it can reach out to other places $this->_createURLs( $file ); //sets the plugin version for backwards compat checks $this->_version = $version; $this->_options = RedactorOptions::get_instance()->get_options(); } /** * Helper function on whether SCRIPT_DEBUG is set * * Returns whether or not SCRIPT_DEBUG is set or not * * @static * @since 0.0.1 * @return boolean */ static function is_script_debug(){ return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; } /** * Helper function on whether WP_DEBUG is set * * Returns whether or not WP_DEBUG is set. * * @static * @since 0.0.1 * @return boolean */ static function is_wordpress_debug(){ return defined( 'WP_DEBUG' ) && WP_DEBUG; } /** * Create and initializes paths for the plugin * * Stores all of the paths that the plugin will use to access the world * * @access private * @since 0.0.1 * @return void */ private function _createURLs( $file ) { $this->_file = $file; $this->_dir = dirname($file); $this->_assets_dir = trailingslashit( $this->_dir ); $this->_assets_url = esc_url( trailingslashit( plugins_url( '', $this->_file ) ) ); $this->_languages_dir = trailingslashit( $this->_dir ) . 'languages'; } /** * Not allowed * * The plugin is a singleton so don't allow cloning. * * @access private * @since 0.0.1 * @return void */ final private function __clone() {} /** * Creates the database table that contains the redact rules and patterns. * * @static * @access public * @since 0.0.1 * @return void */ static function install_database(){ global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $sql = "CREATE TABLE {$table_name} ( \n". " id int(11) NOT NULL AUTO_INCREMENT , \n". " dt_added datetime DEFAULT NOW() NOT NULL , \n". " rx_redaction varchar(500) DEFAULT '' NOT NULL , \n". " str_username varchar(50) DEFAULT '' NOT NULL , \n". " str_groups varchar(500) DEFAULT '' NOT NULL \n". ") $charset_collate; "; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); //if wordpress is in debug mode then insert some default redaction values for testing. /* if(RedactorModel::is_wordpress_debug()){ $rows[] = array( 'rx_redaction' => 'RedactTest', 'str_username' => 'user', 'str_groups' => 'administrator,editor' ); $rows[] = array( 'rx_redaction' => 'RedactorTest', 'str_username' => 'user', 'str_groups' => 'administrator,editor' ); $rowcount = $wpdb->get_results("select count(1) as rowcount from $table_name"); if($rowcount && count($rowcount) > 0 && $rowcount[0]->rowcount == 0){ foreach($rows as $row){ $wpdb->insert($table_name, $row, array( '%s' )); } } }*/ add_option( 'wordactor_version', datasync_PHP_MINIMUM_VERSION ); } /** * Queries the database for matching redactions and returns the raw row object array. * * @access public * @param string $strContent * @since 0.0.1 * @return array $rows Raw records of matching redactions. */ private function _getMatchingRedactsFromDatabase($strContent){ global $wpdb; $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $sql = $wpdb->prepare( "select * from $table_name where %s RLIKE `rx_redaction`", $strContent ); $rows = $wpdb->get_results( $sql ); return $rows; } /** * Accepts an array of strings and translates them into what should be used as the redacted text. This returns * redacted versions of the content no matter what the permissions are. * * @access public * @param array $arrMatches The array of strings that are content that matches redacted text. * @since 0.0.1 * @return array $arrRedacted The array of strings translated into what should be used as redacted text. */ public function convertToRedactStrings($arrMatches){ if(is_string($arrMatches)){ return preg_replace("/[^\s]/", "█", $arrMatches); } if(!is_array($arrMatches)){ $arrMatches = array(''.$arrMatches.''); } $arrRedacted = array(); foreach($arrMatches as $strMatch){ $arrRedacted[] = preg_replace("/[^\s]/", datasync_FULL_BLOCK, $strMatch); } return $arrRedacted; } /** * Accepts a string to have content redacted. Queries the database for * matching redactions and returns a multidimensional array of content, * redacted content, pattern, and permissions. Each row in the array was * a matching redaction rule in the database. * * @access public * $param string $strContent The content to match against. * @since 0.0.1 * @return array $arrMatches The matches array for filtering content. */ public function getRedactRules($strContent){ $rows = $this->_getMatchingRedactsFromDatabase( $strContent ); return $rows; } /** * Returns the total number of rule records in the database. * @since 0.0.1 * @access public * @global type $wpdb * @return int */ public function getRuleRowCount(){ global $wpdb; $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $countresults = $wpdb->get_results( " SELECT count(*) AS rowcount FROM $table_name " ); $rowcount = $countresults[0]->rowcount; return $rowcount; } /** * Returns the raw database object returned from querying rules from the database. * @global type $wpdb * @param int $offset * @param int $limit * @return object */ public function getRawRuleRecords($offset = 0, $limit = 50){ global $wpdb; $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; //parameter check if(is_null($offset) || !is_int($offset)) $offset = 0; if(is_null($limit) || !is_int($limit)) $limit = 50; $offset = ($offset >= 0 )? $offset : 0; $limit = ($limit > 0 )? $limit : 50; $sql = $wpdb->prepare( " SELECT id, dt_added, rx_redaction, str_username, str_groups FROM $table_name LIMIT %d, %d ", $offset, $limit ); $results = $wpdb->get_results( $sql ); return $results; } /** * Adds a rule to the database. On success, it queries the resulting id and returns * the inserted row as a database object. * @global type $wpdb * @param string $rule * @param string $permissions * @param string $user * @return object The raw row created. */ public function createRule($rule, $permissions, $user){ global $wpdb; $results = array(); if( is_null($rule) || !is_string($rule) ) $rule = ''; if( is_null($permissions) || !is_string($permissions) ) $permissions = ''; if( is_null($user) || !is_string($user) ) $user = ''; //prepare the data $data = array( 'rx_redaction' => $rule, 'str_username' => $user, 'str_groups' => $permissions ); $format = array( '%s', '%s', '%s' ); //set the tablename and insert the data $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $success = $wpdb->insert($table_name, $data, $format); if($success){ $id = $wpdb->insert_id; $sql = $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id); $results = $wpdb->get_row($sql, ARRAY_A ); } return $results; } /** * Updates a row in the rules table with the provided id and returns the changed row. * @global type $wpdb * @param int $id * @param string $rule * @param string $permissions * @param string $user * @return array */ public function updateRule($id, $rule, $permissions, $user){ global $wpdb; $results = array(); //translate a string to an integer if( is_string($id) ){ $id = intval($id, 10); } //if not a valid id just return a blank array if(!is_integer($id) || $id <= 0) return $results; if( is_null($rule) || !is_string($rule) ) $rule = ''; if( is_null($permissions) || !is_string($permissions) ) $permissions = ''; if( is_null($user) || !is_string($user) ) $user = ''; //prepare the data $data = array( 'rx_redaction' => $rule, 'str_username' => $user, 'str_groups' => $permissions, 'dt_added' => current_time('mysql', 1) ); $format = array( '%s', '%s', '%s', '%s' ); //replace where id = value $where = array( 'id' => $id ); $whereformat = array( '%d' ); //set the tablename and update the matching row $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $success = $wpdb->update($table_name, $data, $where, $format, $whereformat); if($success){ $sql = $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id); $results = $wpdb->get_row( $sql, ARRAY_A ); } return $results; } /** * Deletes a rule from the database with the matching id. * @global type $wpdb * @param int $id * @return boolean */ public function deleteRule( $id ){ global $wpdb; //convert from string to id if( is_string($id) ){ $id = intval($id, 10); } //checks if id is an integer if( !is_integer($id) || $id <= 0){ return false; } $data = array( 'id' => $id ); $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; return $wpdb->delete($table_name, $data, '%d'); } };