|
From: Jon O. <jon...@us...> - 2006-08-01 21:06:16
|
Update of /cvsroot/mxbb/core/includes In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24654/includes Modified Files: mx_functions_tools.php page_header.php Log Message: - A few generic classes added to the functions_tools file, for modules - cleaned up lang files Index: page_header.php =================================================================== RCS file: /cvsroot/mxbb/core/includes/page_header.php,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** page_header.php 27 Jun 2006 18:12:22 -0000 1.38 --- page_header.php 1 Aug 2006 21:06:13 -0000 1.39 *************** *** 337,342 **** 'TEMPLATE_ROOT_PATH' => TEMPLATE_ROOT_PATH, ! 'L_FORUM' => $lang['Forum'], ! 'L_HOME' => $lang['Home Page'], 'U_INDEX_FORUM' => append_sid(PORTAL_URL . 'index.' . $phpEx . '?page=2'), --- 337,342 ---- 'TEMPLATE_ROOT_PATH' => TEMPLATE_ROOT_PATH, ! 'L_HOME' => $lang['MX_home'], ! 'L_FORUM' => $lang['MX_forum'], 'U_INDEX_FORUM' => append_sid(PORTAL_URL . 'index.' . $phpEx . '?page=2'), Index: mx_functions_tools.php =================================================================== RCS file: /cvsroot/mxbb/core/includes/mx_functions_tools.php,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** mx_functions_tools.php 5 Jul 2006 22:51:13 -0000 1.5 --- mx_functions_tools.php 1 Aug 2006 21:06:13 -0000 1.6 *************** *** 1982,1986 **** // Compose phpBB post header // ! $this->temp_url = PORTAL_URL . this_kb_mxurl("mode=" . "article&k=" . $this->data['item_id'], false, true); $this->auto_message = "[b]" . $this->langs['item_title'] . ":[/b] " . $this->data['item_title'] . "\n"; --- 1982,1986 ---- // Compose phpBB post header // ! //$this->temp_url = PORTAL_URL . this_kb_mxurl("mode=" . "article&k=" . $this->data['item_id'], false, true); $this->auto_message = "[b]" . $this->langs['item_title'] . ":[/b] " . $this->data['item_title'] . "\n"; *************** *** 2111,2113 **** --- 2111,2937 ---- } } + + /** + * Generic module cache. + * + */ + class module_cache + { + var $vars = ''; + var $vars_ts = array(); + var $modified = false; + + /** + * Enter description here... + * + * @return module_cache + */ + function module_cache($dir=false) + { + global $phpbb_root_path; + global $mx_root_path, $module_root_path, $is_block, $phpEx; + + if (!$dir) + { + mx_message_die(GENERAL_ERROR, 'The module cache need a init dir.'); + } + + $this->cache_dir = $dir . 'cache/'; + } + + /** + * Enter description here... + * + */ + function load() + { + global $phpEx; + @include( $this->cache_dir . 'data_global.' . $phpEx ); + } + + /** + * Enter description here... + * + */ + function unload() + { + $this->save(); + unset( $this->vars ); + unset( $this->vars_ts ); + } + + /** + * Enter description here... + * + */ + function save() + { + if ( !$this->modified ) + { + return; + } + + global $phpEx; + $file = '<?php $this->vars=' . $this->format_array( $this->vars ) . ";\n\$this->vars_ts=" . $this->format_array( $this->vars_ts ) . ' ?>'; + + if ( $fp = @fopen( $this->cache_dir . 'data_global.' . $phpEx, 'wb' ) ) + { + @flock( $fp, LOCK_EX ); + fwrite( $fp, $file ); + @flock( $fp, LOCK_UN ); + fclose( $fp ); + } + } + + /** + * Enter description here... + * + * @param unknown_type $expire_time + */ + function tidy( $expire_time = 0 ) + { + global $phpEx; + + $dir = opendir( $this->cache_dir ); + while ( $entry = readdir( $dir ) ) + { + if ( $entry{0} == '.' || substr( $entry, 0, 4 ) != 'sql_' ) + { + continue; + } + + if ( time() - $expire_time >= filemtime( $this->cache_dir . $entry ) ) + { + unlink( $this->cache_dir . $entry ); + } + } + + if ( file_exists( $this->cache_dir . 'data_global.' . $phpEx ) ) + { + foreach ( $this->vars_ts as $varname => $timestamp ) + { + if ( time() - $expire_time >= $timestamp ) + { + $this->destroy( $varname ); + } + } + } + else + { + $this->vars = $this->vars_ts = array(); + $this->modified = true; + } + } + + /** + * Enter description here... + * + * @param unknown_type $varname + * @param unknown_type $expire_time + * @return unknown + */ + function get( $varname, $expire_time = 0 ) + { + return ( $this->exists( $varname, $expire_time ) ) ? $this->vars[$varname] : null; + } + + /** + * Enter description here... + * + * @param unknown_type $varname + * @param unknown_type $var + */ + function put( $varname, $var ) + { + $this->vars[$varname] = $var; + $this->vars_ts[$varname] = time(); + $this->modified = true; + } + + /** + * Enter description here... + * + * @param unknown_type $varname + */ + function destroy( $varname ) + { + if ( isset( $this->vars[$varname] ) ) + { + $this->modified = true; + unset( $this->vars[$varname] ); + unset( $this->vars_ts[$varname] ); + } + } + + /** + * Enter description here... + * + * @param unknown_type $varname + * @param unknown_type $expire_time + * @return unknown + */ + function exists( $varname, $expire_time = 0 ) + { + if ( !is_array( $this->vars ) ) + { + $this->load(); + } + + if ( $expire_time > 0 && isset( $this->vars_ts[$varname] ) ) + { + if ( $this->vars_ts[$varname] <= time() - $expire_time ) + { + $this->destroy( $varname ); + return false; + } + } + + return isset( $this->vars[$varname] ); + } + + /** + * Enter description here... + * + * @param unknown_type $array + * @return unknown + */ + function format_array( $array ) + { + $lines = array(); + foreach ( $array as $k => $v ) + { + if ( is_array( $v ) ) + { + $lines[] = "'$k'=>" . $this->format_array( $v ); + }elseif ( is_int( $v ) ) + { + $lines[] = "'$k'=>$v"; + }elseif ( is_bool( $v ) ) + { + $lines[] = "'$k'=>" . ( ( $v ) ? 'TRUE' : 'FALSE' ); + } + else + { + $lines[] = "'$k'=>'" . str_replace( "'", "\'", str_replace( '\\', '\\\\', $v ) ) . "'"; + } + } + return 'array(' . implode( ',', $lines ) . ')'; + } + } + + /** + * This is a generic class for custom fields. + * + */ + class mx_custom_field + { + var $field_rowset = array(); + var $field_data_rowset = array(); + + var $custom_table = KB_CUSTOM_TABLE; + var $custom_data_table = KB_CUSTOM_DATA_TABLE; + + /** + * Constructor. + * + * @return mx_custom_field + */ + function mx_custom_field($custom_table, $custom_data_table) + { + $this->custom_table = $custom_table; + $this->custom_data_table = $custom_data_table; + } + + /** + * prepare data + * + */ + function init() + { + global $db; + + $sql = "SELECT * + FROM " . $this->custom_table . " + ORDER BY field_order ASC"; + + if ( !( $result = $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Couldnt Query Custom field', '', __LINE__, __FILE__, $sql ); + } + + while ( $row = $db->sql_fetchrow( $result ) ) + { + $this->field_rowset[$row['custom_id']] = $row; + } + unset( $row ); + $db->sql_freeresult( $result ); + + $sql = "SELECT * + FROM " . $this->custom_data_table; + + if ( !( $result = $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Couldnt Query Custom field', '', __LINE__, __FILE__, $sql ); + } + + while ( $row = $db->sql_fetchrow( $result ) ) + { + $this->field_data_rowset[$row['customdata_file']][$row['customdata_custom']] = $row; + } + + unset( $row ); + + $db->sql_freeresult( $result ); + } + + /** + * check if there is a data in the database. + * + * @return unknown + */ + function field_data_exist() + { + if ( !empty( $this->field_data_rowset ) ) + { + return true; + } + return false; + } + + /** + * Enter description here... + * + * @return unknown + */ + function field_exist() + { + if ( !empty( $this->field_rowset ) ) + { + return true; + } + return false; + } + + /** + * display data in the comment. + * + * @param unknown_type $file_id + * @return unknown + */ + function add_comment( $file_id ) + { + global $template; + if ( $this->field_data_exist() ) + { + if ( isset( $this->field_data_rowset[$file_id] ) ) + { + $message = ''; + foreach( $this->field_data_rowset[$file_id] as $field_id => $data ) + { + if ( !empty( $data['data'] ) ) + { + switch ( $this->field_rowset[$field_id]['field_type'] ) + { + case INPUT: + case TEXTAREA: + case RADIO: + case SELECT: + $field_data = $data['data']; + break; + case SELECT_MULTIPLE: + case CHECKBOX: + $field_data = @implode( ', ', unserialize( $data['data'] ) ); + break; + } + $message .= "\n" . "[b]" . $this->field_rowset[$field_id]['custom_name'] . ":[/b] " . $field_data . "\n"; + } + else + { + global $db; + + $sql = "DELETE FROM " . $this->custom_data_table . " + WHERE customdata_file = '$file_id' + AND customdata_custom = '$field_id'"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not delete custom data', '', __LINE__, __FILE__, $sql ); + } + } + } + return $message; + } + else + { + return false; + } + } + else + { + return false; + } + } + + /** + * display data in the file page. + * + * @param unknown_type $file_id + * @return unknown + */ + function display_data( $file_id ) + { + global $template; + if ( $this->field_data_exist() ) + { + if ( isset( $this->field_data_rowset[$file_id] ) ) + { + foreach( $this->field_data_rowset[$file_id] as $field_id => $data ) + { + if ( !empty( $data['data'] ) ) + { + switch ( $this->field_rowset[$field_id]['field_type'] ) + { + case INPUT: + case TEXTAREA: + case RADIO: + case SELECT: + $field_data = $data['data']; + break; + case SELECT_MULTIPLE: + case CHECKBOX: + $field_data = @implode( ', ', unserialize( $data['data'] ) ); + break; + } + + $template->assign_block_vars( 'custom_field', array( + 'CUSTOM_NAME' => $this->field_rowset[$field_id]['custom_name'], + 'DATA' => $field_data ) + ); + } + else + { + global $db; + + $sql = "DELETE FROM " . $this->custom_data_table . " + WHERE customdata_file = '$file_id' + AND customdata_custom = '$field_id'"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not delete custom data', '', __LINE__, __FILE__, $sql ); + } + } + } + } + else + { + return false; + } + } + else + { + return false; + } + } + + /** + * display custom field and data in the add/edit page. + * + * @param unknown_type $file_id + * @return unknown + */ + function display_edit( $file_id = false ) + { + $return = false; + if ( $this->field_exist() ) + { + foreach( $this->field_rowset as $field_id => $field_data ) + { + switch ( $field_data['field_type'] ) + { + case INPUT: + $this->display_edit_input( $file_id, $field_id, $field_data ); + break; + case TEXTAREA: + $this->display_edit_textarea( $file_id, $field_id, $field_data ); + break; + case RADIO: + $this->display_edit_radio( $file_id, $field_id, $field_data ); + break; + case SELECT: + $this->display_edit_select( $file_id, $field_id, $field_data ); + break; + case SELECT_MULTIPLE: + $this->display_edit_select_multiple( $file_id, $field_id, $field_data ); + break; + case CHECKBOX: + $this->display_edit_checkbox( $file_id, $field_id, $field_data ); + break; + } + + $return = true; + } + } + return $return; + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_input( $file_id, $field_id, $field_data ) + { + global $template; + $field_value_temp = (!empty( $this->field_data_rowset[$file_id][$field_id]['data'] )) ? $this->field_data_rowset[$file_id][$field_id]['data'] : ''; + $field_value = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $field_value_temp ; + $template->assign_block_vars( 'input', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'], + 'FIELD_VALUE' => $field_value ) + ); + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_textarea( $file_id, $field_id, $field_data ) + { + global $template; + $field_value_temp = ( !empty( $this->field_data_rowset[$file_id][$field_id]['data'] ) ) ? $this->field_data_rowset[$file_id][$field_id]['data'] : ''; + $field_value = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $field_value_temp ; + $template->assign_block_vars( 'textarea', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'], + 'FIELD_VALUE' => $field_value ) + ); + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_radio( $file_id, $field_id, $field_data ) + { + global $template; + $template->assign_block_vars( 'radio', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'] ) + ); + + $data_temp = ( !empty( $this->field_data_rowset[$file_id][$field_id]['data'] ) ) ? $this->field_data_rowset[$file_id][$field_id]['data'] : array(); + $data = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $data_temp ; + $field_datas = ( !empty( $field_data['data'] ) ) ? unserialize( stripslashes( $field_data['data'] ) ) : array(); + + if ( !empty( $field_datas ) ) + { + foreach( $field_datas as $key => $value ) + { + $template->assign_block_vars( 'radio.row', array( + 'FIELD_VALUE' => $value, + 'FIELD_SELECTED' => ( $data == $value ) ? ' checked="checked"' : '' ) + ); + } + } + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_select( $file_id, $field_id, $field_data ) + { + global $template; + $template->assign_block_vars( 'select', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'] ) + ); + + $data_temp = ( !empty( $this->field_data_rowset[$file_id][$field_id]['data'] ) ) ? $this->field_data_rowset[$file_id][$field_id]['data'] : ''; + $data = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $data_temp ; + $field_datas = ( !empty( $field_data['data'] ) ) ? unserialize( stripslashes( $field_data['data'] ) ) : array(); + + if ( !empty( $field_datas ) ) + { + foreach( $field_datas as $key => $value ) + { + $template->assign_block_vars( 'select.row', array( + 'FIELD_VALUE' => $value, + 'FIELD_SELECTED' => ( $data == $value ) ? ' selected="selected"' : '' ) + ); + } + } + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_select_multiple( $file_id, $field_id, $field_data ) + { + global $template; + $template->assign_block_vars( 'select_multiple', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'] ) + ); + + $data_temp = ( !empty( $this->field_data_rowset[$file_id][$field_id]['data'] ) ) ? unserialize( $this->field_data_rowset[$file_id][$field_id]['data'] ) : array(); + $data = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $data_temp ; + $field_datas = ( !empty( $field_data['data'] ) ) ? unserialize( stripslashes( $field_data['data'] ) ) : array(); + + if ( !empty( $field_datas ) ) + { + foreach( $field_datas as $key => $value ) + { + $selected = ''; + foreach( $data as $field_value ) + { + if ( $field_value == $value ) + { + $selected = ' selected="selected"'; + break; + } + } + $template->assign_block_vars( 'select_multiple.row', array( + 'FIELD_VALUE' => $value, + 'FIELD_SELECTED' => $selected ) + ); + } + } + } + + /** + * Enter description here... + * + * @param unknown_type $file_id + * @param unknown_type $field_id + * @param unknown_type $field_data + */ + function display_edit_checkbox( $file_id, $field_id, $field_data ) + { + global $template; + $template->assign_block_vars( 'checkbox', array( + 'FIELD_NAME' => $field_data['custom_name'], + 'FIELD_ID' => $field_data['custom_id'], + 'FIELD_DESCRIPTION' => $field_data['custom_description'] ) + ); + + $data_temp = ( !empty( $this->field_data_rowset[$file_id][$field_id]['data'] ) ) ? unserialize( $this->field_data_rowset[$file_id][$field_id]['data'] ) : array(); + $data = !empty( $_POST['field'][$field_data['custom_id']] ) ? $_POST['field'][$field_data['custom_id']] : $data_temp ; + $field_datas = ( !empty( $field_data['data'] ) ) ? unserialize( stripslashes( $field_data['data'] ) ) : array(); + + if ( !empty( $field_datas ) ) + { + foreach( $field_datas as $key => $value ) + { + $checked = ''; + foreach( $data as $field_value ) + { + if ( $field_value == $value ) + { + $checked = ' checked'; + break; + } + } + $template->assign_block_vars( 'checkbox.row', array( + 'FIELD_VALUE' => $value, + 'FIELD_CHECKED' => $checked ) + ); + } + } + } + + /** + * Enter description here... + * + * @param unknown_type $field_type + * @param unknown_type $field_id + */ + function update_add_field( $field_type, $field_id = false ) + { + global $db, $db, $_POST, $lang; + + $field_name = ( isset( $_POST['field_name'] ) ) ? htmlspecialchars( $_POST['field_name'] ) : ''; + $field_desc = ( isset( $_POST['field_desc'] ) ) ? htmlspecialchars( $_POST['field_desc'] ) : ''; + $regex = ( isset( $_POST['regex'] ) ) ? $_POST['regex'] : ''; + $data = ( isset( $_POST['data'] ) ) ? $_POST['data'] : ''; + $field_order = ( isset( $_POST['field_order'] ) ) ? $_POST['field_order'] : ''; + + if ( $field_id ) + { + $field_order = ( isset( $_POST['field_order'] ) ) ? intval( $_POST['field_order'] ) : ''; + } + + if ( !empty( $data ) ) + { + $data = explode( "\n", htmlspecialchars( trim( $data ) ) ); + + foreach( $data as $key => $value ) + { + $data[$key] = trim( $value ); + } + $data = addslashes( serialize( $data ) ); + } + + if ( empty( $field_name ) ) + { + mx_message_die( GENERAL_ERROR, $lang['Missing_field'] ); + } + + if ( ( ( $field_type != INPUT && $field_type != TEXTAREA ) && empty( $data ) ) ) + { + mx_message_die( GENERAL_ERROR, $lang['Missing_field'] ); + } + + if ( !$field_id ) + { + $sql = "INSERT INTO " . $this->custom_table . " (custom_name, custom_description, data, regex, field_type) + VALUES('" . $field_name . "', '" . $field_desc . "', '" . $data . "', '" . $regex . "', '" . $field_type . "')"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not add the new fields', '', __LINE__, __FILE__, $sql ); + } + + $field_id = $db->sql_nextid(); + + $sql = "UPDATE " . $this->custom_table . " + SET field_order = '$field_id' + WHERE custom_id = $field_id"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not set the order for the giving field', '', __LINE__, __FILE__, $sql ); + } + } + else + { + $sql = "UPDATE " . $this->custom_table . " + SET custom_name = '$field_name', custom_description = '$field_desc', data = '$data', regex = '$regex', field_order='$field_order' + WHERE custom_id = $field_id"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not update information for the giving field', '', __LINE__, __FILE__, $sql ); + } + } + } + + /** + * Enter description here... + * + * @param unknown_type $field_id + */ + function delete_field( $field_id ) + { + global $db; + + $sql = "DELETE FROM " . $this->custom_data_table . " + WHERE customdata_custom = '$field_id'"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not delete custom data', '', __LINE__, __FILE__, $sql ); + } + + $sql = "DELETE FROM " . $this->custom_table . " + WHERE custom_id = '$field_id'"; + + if ( !( $db->sql_query( $sql ) ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not delete the selected field', '', __LINE__, __FILE__, $sql ); + } + } + + /** + * Enter description here... + * + * @param unknown_type $field_id + * @return unknown + */ + function get_field_data( $field_id ) + { + $return_array = $this->field_rowset[$field_id]; + $return_array['data'] = !empty( $return_array['data'] ) ? implode( "\n", unserialize( stripslashes( $return_array['data'] ) ) ) : ''; + return $return_array; + } + + /** + * file data in custom field operations. + * + * @param unknown_type $file_id + */ + function file_update_data( $file_id ) + { + global $_POST, $db; + $field = ( isset( $_POST['field'] ) ) ? $_POST['field'] : ''; + if ( !empty( $field ) ) + { + foreach( $field as $field_id => $field_data ) + { + if ( !empty( $this->field_rowset[$field_id]['regex'] ) ) + { + if ( !preg_match( '#' . $this->field_rowset[$field_id]['regex'] . '#siU', $field_data ) ) + { + $field_data = ''; + } + } + + switch ( $this->field_rowset[$field_id]['field_type'] ) + { + case INPUT: + case TEXTAREA: + case RADIO: + case SELECT: + $data = htmlspecialchars( $field_data ); + break; + case SELECT_MULTIPLE: + case CHECKBOX: + $data = addslashes( serialize( $field_data ) ); + break; + } + + $sql = "DELETE FROM " . $this->custom_data_table . " + WHERE customdata_file = '$file_id' + AND customdata_custom = '$field_id'"; + + if ( !$db->sql_query( $sql ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not delete data from custom data table', '', __LINE__, __FILE__, $sql ); + } + + if ( !empty( $data ) ) + { + $sql = "INSERT INTO " . $this->custom_data_table . " (customdata_file, customdata_custom, data) + VALUES('$file_id', '$field_id', '$data')"; + + if ( !$db->sql_query( $sql ) ) + { + mx_message_die( GENERAL_ERROR, 'Could not add additional data', '', __LINE__, __FILE__, $sql ); + } + } + } + } + } + } ?> \ No newline at end of file |