NOW()))"; $service_info_filter = "((service_info.issue_date = '0000-00-00' OR service_info.issue_date <= NOW()) AND (service_info.expire_date = '0000-00-00' OR service_info.expire_date > NOW()))"; //----- forms --------------------------------------------------------------- $CategoryForm = array ( 'id' => array ( ), 'name' => array ( ), 'topic_id' => array ( ), 'keywords' => array ( 'join' => 'topic.keywords:topic.id=topic_id' ), 'description' => array ( ) ); $CategoryList = array ( 'id' => array ( ), 'name' => array ( ) ); $ContentForm = array ( 'id' => array ( ), 'section_id' => array ( ), 'topic_id' => array ( ), 'keywords' => array ( 'join' => 'topic.keywords:topic.id=topic_id' ), 'issue_date' => array ( ), 'expire_date' => array ( ), 'content_date' => array ( 'join' => "if(issue_date = '0000-00-00', '', date_format(issue_date, '%d.%m.%Y'))" ), 'headline' => array ( ), 'title' => array ( ), 'subtitle' => array ( ), 'blurb' => array ( ), 'body' => array ( ), 'more' => array ( ), 'hint' => array ( ) ); $ContentList = array ( 'id' => array ( ), 'section_id' => array ( ), 'issue_date' => array ( ), 'expire_date' => array ( ), 'content_date' => array ( 'join' => "if(issue_date = '0000-00-00', '', date_format(issue_date, '%d.%m.%Y'))" ), 'title' => array ( ), 'blurb' => array ( ) ); $SectionForm = array ( 'id' => array ( ), 'topic_id' => array ( ), 'keywords' => array ( 'join' => 'topic.keywords:topic.id=topic_id' ), 'sortorder' => array ( ), 'title' => array ( ), 'single' => array ( ), 'pagepath' => array ( ), 'body' => array ( ), 'more' => array ( ), 'image' => array ( ), 'image_x' => array ( ), 'image_y' => array ( ) ); $SectionList = array ( 'id' => array ( ), 'title' => array ( ) ); //----- supplemental functions ---------------------------------------------- define ('ROWS_PER_PAGE', 20); define ('PAGES_INDEXED', 20); function PageIndex($rows, $page=1, $vars='') { $result = ''; if ($rows > ROWS_PER_PAGE) { $page_cnt = floor(($rows - 1) / ROWS_PER_PAGE) + 1; $page_min = max($page - intval(PAGES_INDEXED / 2), 1); $page_max = min($page + intval(PAGES_INDEXED / 2), $page_cnt); $result .= "

\n"; if (1 < $page_min) $result .= "<< \n"; if (1 < $page) $result .= "< \n"; for ($i = $page_min; $i <= $page_max; $i++) { if ($i != $page) $result .= "$i \n"; else $result .= "$i \n"; } if ($page < $page_cnt) $result .= "> \n"; if ($page_max < $page_cnt) $result .= ">>\n"; $result .= "

\n"; } return $result; } function PageLimit($page) { return max(0,($page - 1) * ROWS_PER_PAGE).",".ROWS_PER_PAGE; } ?> DD.MM.YYYY // ----- version 1.09 ----- // b7_text2html added // ----- version 1.10 ----- // unit supressed on output-fields when no data available // b7_form_file: DOCUMENT_ROOT support when set // obsolete functions removed // ----- version 1.11 ------ // b7_db_write introduced // b7_form_field types 'html' & 'show' bugfix for empty value // ----- version 1.12 ------ // b7_form_read: error on no data removed // b7_form_write: index_default, new parameter for manual index creation // ----- version 1.13 ------ // b7_form_file: validation added // b7_cent2text: function added // b7_euro2text: function added // b7_form_field, b7_list_field: surpress output on euro/cent-null-fields // ----- version 1.14 ------ // b7_list_read: limit argument added // b7_form_file: bugfix on imagesize // ----- version 1.15 ------ // b7_menu: rawtext without link on integer index // b7_form_create_inner: supports serialization for multiple usage // ----- version 1.16 ------ // b7_array_merge: function added // b7_list_create: order property added // b7_element_hidden: added b7_text2html conversion for compatibility // ----- version 1.17 ------ // b7_form_read/write / b7_list_read: type 'euro' conversion removed // b7_form_field / b7_list_cell: added 'href' with replace support // ----- version 1.18 ------ // b7_list_create_inner: group title supports extended evaluation (list) // ----- version 1.19 ------ // b7_list_js_array: support for unnamed arrays // new field type: currency // new field attribute: readonly // b7_db_count: join supported // ----- version 1.20 ------ // b7_replace: zerolength added for replacing with empty variables //=========================================================================== define ('B7_LIB', 1.20); function b7_lib() { return B7_LIB; } //--------------------------------------------------------------------------- function b7_array_merge($arr_1, $arr_2) // like array_merge, but keeps keys { $result = array(); if (is_array($arr_1)) for (reset($arr_1); list($k, $v) = each($arr_1); ) $result[$k] = $v; if (is_array($arr_2)) for (reset($arr_2); list($k, $v) = each($arr_2); ) $result[$k] = $v; return $result; } function b7_explode($seperator, $operand) // like explode, but works with empty string { return strlen($operand) ? explode($seperator, $operand) : array(); } function b7_implode($seperator, $operand) // like implode, but works with empty array { return sizeof($operand) ? implode($seperator, $operand) : ""; } function b7_check_email($my_email) // syntax check of email address format { $result = $my_email = trim(strtolower(str_replace("\n", " ", $my_email))); if (!ereg('^([a-z0-9_]|\\-|\\.)+'.'@'.'(([a-z0-9_]|\\-)+\\.)*'.'(([a-z0-9_]|\\-){2,100}\\.){1}'.'[a-z]{2,4}$',$my_email)) $result = ""; return $result; } function b7_date2text($date) { $result = ''; $temp = b7_explode(' ', $date); // seperate useless time if (sizeof($a = b7_explode('.', $temp[0])) >= 3) list($d, $m, $y) = $a; // format = tag.monat.jahr else if (sizeof($a = b7_explode('-', $temp[0])) >= 3) list($y, $m, $d) = $a; // format = jahr-monat-tag else $d = $m = $y = 0; if (checkdate($m, $d, $y)) $result = sprintf('%02d.%02d.%04d', intval($d), intval($m), intval($y)); return $result; } function b7_text2date($text) { $result = ''; if (sizeof($a = b7_explode('.', $text)) >= 3) list($d, $m, $y) = $a; // format = tag.monat.jahr else if (sizeof($a = b7_explode('-', $text)) >= 3) list($y, $m, $d) = $a; // format = jahr-monat-tag else $d = $m = $y = 0; if (checkdate($m, $d, $y)) $result = sprintf('%04d-%02d-%02d', intval($y), intval($m), intval($d)); return $result; } function b7_text2html($text) { return stripslashes(htmlspecialchars($text)); } function b7_euro2text($euro) { $result = ''; if (strlen($euro)) $result = number_format($euro, 2, ',', '.'); return $result; } function b7_cent2text($cent) { $result = ''; if (strlen($cent)) $result = b7_euro2text($cent / 100); return $result; } function b7_currency2text($currency) { return number_format($currency / 100, 2, ',', ''); } function b7_text2currency($text) { return round(str_replace(',', '.', $text) * 100); } //----- menu functions ------------------------------------------------------ function b7_menu($menu, $id, $current='') { $result = ''; $result .= "
\n"; $result .= "\n"; $result .= "
\n"; return $result; }; function b7_menu_mode($menu, $id, $current='') { global $PHP_SELF; for ($temp = array(), reset($menu); list($k, $v) = each($menu); ) $temp[$v] = $PHP_SELF."?mode=".$k; return b7_menu($temp, $id, $current); }; //----- sql functions ------------------------------------------------------- function b7_sql_delete($name, $where = '', $join = null) { $sql_del = $name; $sql_join = ''; $sql_where = $where ? "WHERE $where" : ''; if (isset($join)) { if (is_array($join)) $temp = $join; else $temp = array($join); for (reset($temp); list($k, $v) = each($temp); ) { if (sizeof($j = b7_explode(':', $v)) >= 2) { switch (sizeof($j)) { case 2: $sql_del .= ",\n{$j[0]}"; $sql_join .= "\nLEFT JOIN {$j[0]} ON {$j[1]}"; break; default: $sql_del .= ",\n{$j[2]}"; $sql_join .= "\nLEFT JOIN {$j[0]} AS {$j[2]} ON {$j[1]}"; break; } } } } return "DELETE $sql_del FROM $name $sql_join $sql_where "; } function b7_sql_select($info, $name, $where='', $order='', $limit='') { $sql_sel = ''; $sql_join = ''; $sql_group = ''; $sql_where = $where ? "WHERE $where" : ''; $sql_order = $order ? "ORDER BY $order" : ''; $sql_limit = $limit ? "LIMIT $limit" : ''; for (reset($info), $n = ''; list($k, $v) = each($info); ) { if (empty($v['type']) || !in_array($v['type'], array('static', 'private', 'button', 'button_ex', 'image', 'submit', 'result'))) { if (isset($v['join']) && strlen($v['join'])) // join => 'table.field|expression[:join][:table][:alias]' { if (sizeof($j = b7_explode(':', $v['join'])) >= 2) { switch (sizeof($j)) { case 2: if (sizeof($i = b7_explode('.', $j[0])) >= 2) $sql_join .= "\nLEFT JOIN {$i[0]} ON {$j[1]}"; break; case 3: $sql_join .= "\nLEFT JOIN {$j[2]} ON {$j[1]}"; break; default: $sql_join .= "\nLEFT JOIN {$j[2]} AS {$j[3]} ON {$j[1]}"; break; } } $n = strlen($j[0]) ? "{$j[0]} AS $k" : ''; } else $n = strlen($k) ? "$name.$k AS $k" : ''; if (!empty($v['group'])) // group => 'table.field' { if (strlen($sql_group)) $sql_group .= ','; else $sql_group = 'GROUP BY '; $sql_group .= $v['group']; } if (strlen($n)) // add field to select { if (strlen($sql_sel)) $sql_sel .= ",\n"; $sql_sel .= $n; } } } return "SELECT $sql_sel FROM $name $sql_join $sql_where $sql_group $sql_order $sql_limit "; } //----- database functions -------------------------------------------------- function b7_db_select($db_name = DB_NAME, $db_host = DB_HOST, $db_user = DB_USER, $db_pass = DB_PASS) { $result = null; // due to error detection if ( $link = mysql_connect ($db_host, $db_user, $db_pass) ) $result = mysql_select_db ($db_name, $link); else die ("Verbindungsfehler: ".mysql_error()); return $result; } function b7_db_delete($name, $where, $join = null) { $result = ''; $sql = b7_sql_delete($name, $where, $join); if ( !mysql_query($sql) ) $result = mysql_error(); return $result; } function b7_db_read($name, $key, $value, $where = '', $join = '', $order = '') { $result = null; // due to error detection $info = array($key => array(), $value => array('join' => $join)); $sql = b7_sql_select($info, $name, $where, strlen($order) ? $order : $value); if ($res = mysql_query($sql)) { $result = array(); while ($row = mysql_fetch_assoc($res)) if ($key) $result[$row[$key]] = $row[$value]; else $result[$row[$value]] = $row[$value]; mysql_free_result($res); } return $result; } function b7_db_write($name, $key, $value, $where = '') { $result = ''; $sql_set = "$key = '$value'"; $sql = strlen($where) ? "UPDATE $name SET $sql_set WHERE $where" : "INSERT $name SET $sql_set"; if (!mysql_query($sql)) $result = mysql_error(); return $result; } function b7_db_count($name, $where = '', $join = '') { $result = null; // due to error detection $sql = " SELECT COUNT(*) FROM $name"; if (isset($join) && strlen($join)) // join => 'table.field|expression[:join][:table][:alias]' { if (sizeof($j = b7_explode(':', $join)) >= 2) { switch (sizeof($j)) { case 2: if (sizeof($i = b7_explode('.', $j[0])) >= 2) $sql .= " LEFT JOIN {$i[0]} ON {$j[1]}"; break; case 3: $sql .= " LEFT JOIN {$j[2]} ON {$j[1]}"; break; default: $sql .= " LEFT JOIN {$j[2]} AS {$j[3]} ON {$j[1]}"; break; } } } if (strlen($where)) $sql .= " WHERE $where"; if ($res = mysql_query($sql)) { $result = mysql_result($res, 0); mysql_free_result($res); } return $result; } function b7_db_change_priority($name, $value, $group = '', $priority = 'priority') { $val = abs($value); if ($value > 0) // find succsessor { $sql_where = "$priority > $val"; $sql_order = "$priority ASC"; } else // find predicessor { $sql_where = "$priority < $val"; $sql_order = "$priority DESC"; } if ($group) // add group filter { $sql = " SELECT $group FROM $name WHERE $priority = '$val' "; if ($res = mysql_query($sql)) { if ($row = mysql_fetch_assoc($res)) $sql_where .= " AND $group = '{$row[$group]}'"; mysql_free_result($res); } } $sql = " SELECT $priority FROM $name WHERE $sql_where ORDER BY $sql_order LIMIT 1 "; if ($res = mysql_query($sql)) { if ($row = mysql_fetch_assoc($res)) { $new = $row[$priority]; mysql_query("UPDATE $name SET $priority = 0 WHERE $priority = $val"); mysql_query("UPDATE $name SET $priority = $val WHERE $priority = $new"); mysql_query("UPDATE $name SET $priority = $new WHERE $priority = 0"); } mysql_free_result($res); } } function b7_db_toggle_enabled($name, $index, $id='id', $enabled = 'enabled') { if ($res = mysql_query("SELECT $enabled FROM $name WHERE $id = '$index'")) { if ($row = mysql_fetch_assoc($res)) { $val = $row[$enabled] == '1' ? '0' : '1'; mysql_query("UPDATE $name SET $enabled = '$val' WHERE $id = '$index'"); } mysql_free_result($res); } } //--------------------------------------------------------------------------- function b7_event_handler($data) { $arrEventHandler = array ( 'onBlur', 'onblur', 'onChange', 'onchange', 'onClick', 'onclick', 'onDblClick', 'ondblclick' ); for ($result = '', reset($arrEventHandler); list($k, $v) = each($arrEventHandler); ) if ( isset($data[$v]) ) $result .= " $v=\"{$data[$v]}\""; return $result; } function b7_replace($expr, $data, $mark = '%%', $zerolength = false) { // alle variablen durchsuchen und %%name%% durch wert ersetzen if (strpos($expr, $mark) !== FALSE && isset($data)) for (reset($data); list($k, $v) = each($data); ) if (!is_array($v) && ($zerolength || strlen($v))) $expr = str_replace("$mark{$k}$mark", $v, $expr); return $expr; } function b7_replace_ex($expr, $data, $mark = '%%') { if (strpos($expr = b7_replace($expr, $data), $mark) !== FALSE) $expr = ''; return $expr; } //--------------------------------------------------------------------------- function b7_element_form($text, $action, $name = '') { $n = strlen($name) ? " name=\"$name\"" : ''; $result = "\n"; $result .= $text; $result .= "\n"; return $result; } function b7_element_fieldset($text, $legend = '', $class = '') { $l = strlen($legend) ? "$legend" : ''; $c = strlen($class) ? " class=\"$class\"" : ''; $result = "$l\n"; $result .= $text; $result .= "\n"; return $result; } function b7_element_hidden($k, $v) { $result = ''; if (is_array($v)) for (reset($v); list($k2, $v2) = each($v); ) if (empty($k)) $result .= b7_element_hidden($k2, $v2); else $result .= b7_element_hidden("{$k}[$k2]", $v2); else if (strlen($k)) $result .= ""; return $result; } function b7_element_checkbox($id, $list, $name = '', $data = null, $event = '') { $result = ''; for (reset($list); list($k2, $v2) = each($list); ) { $checked = is_array($data) && in_array($k2, array_keys($data)) ? " checked" : ""; $result .= strlen($result) ? "\n" : ""; $result .= "
"; $result .= "$v2"; $result .= "
"; } return $result; //"
\n$result\n
"; } function b7_element_radio($id, $list, $name = '', $data = null, $event = '') { $result = ''; for (reset($list); list($k2, $v2) = each($list); ) { $checked = isset($data) && $k2 == $data ? " checked" : ""; $result .= strlen($result) ? "\n" : ""; $result .= "
"; $result .= "$v2"; $result .= "
"; } return $result; //"
\n$result\n
"; } function b7_element_select($id, $list, $name = '', $data = null, $event = '') { $result = ""; return $result; } //----- form functions ------------------------------------------------------ function b7_form_field_data($k, $v, $data) { return isset($data[$k]) ? $data[$k] : (isset($v['default']) ? $v['default'] : null); } function b7_form_field_name($k, $v, $name, $serial = '') { if (strlen($name) && (empty($v['type']) || !in_array($v['type'], array('button', 'button_ex', 'image', 'submit')))) $result = strlen($serial) ? $name."[$serial][$k]" : $name."[$k]"; else $result = strlen($serial) ? $k."[$serial]" : $k; return $result; } function b7_form_field_unit($v) { return isset($v['unit']) ? $v['unit'] : (isset($v['type']) && in_array($v['type'], array('euro', 'cent')) ? ' €' : ''); } function b7_form_field($k, $v, $name = '', $data = null, $follow = FALSE, $serial = '') { $result = ''; $d = b7_form_field_data($k, $v, $data); // default | value $n = b7_form_field_name($k, $v, $name, $serial); // variable name $u = b7_form_field_unit($v); // unit extension $e = b7_event_handler($v); // event handler $b = $follow || !empty($v['follow']) ? 'span' : 'div'; // box type $r = !empty($v['readonly']) ? ' readonly' : ''; // readonly // supress unit for output-fields when no data available if (!is_array($d) && !strlen($d) && isset($v['type']) && !in_array($v['type'], array('date', 'text', 'password', 'currency'))) $u = ''; if (isset($v['type'])) { switch ($v['type']) { case 'button_ex': $result .= ""; break; case 'checkbox': if (!is_array($d)) // unserialize data for checkbox $d = array_flip(b7_explode(',', $d)); if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_checkbox($k, $v['list'], $n, $d, $e); break; case 'radio': if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_radio($k, $v['list'], $n, $d, $e); break; case 'select': if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_select($k, $v['list'], $n, $d, $e); break; case 'file': $result .= ""; $result .= ""; break; case 'textarea': $result .= ""; break; case 'date': case 'currency': $result .= ""; break; case 'text': case 'password': case 'submit': case 'button': $result .= ""; break; case 'image': $result .= ""; break; //----- output types ---------------------------------------------------- case 'hidden': case 'private': $result .= b7_element_hidden($n, $d); break; case 'static': case 'custom': if (isset($v['default'])) $d = b7_replace_ex($v['default'], $data); $result .= "<$b class=\"value\" id=\"$k\">$d$u"; break; case 'html': if (isset($v['list']) && is_array($v['list']) && isset($v['list'][$d])) $d = $v['list'][$d]; if (strlen($d)) $result .= "<$b class=\"value\" id=\"$k\">".nl2br($d)."$u"; else $result .= ' '; // nothing to show, so do it the simple way break; case 'show': if (isset($v['list']) && is_array($v['list']) && isset($v['list'][$d])) $d = $v['list'][$d]; if (strlen($d)) $result .= "<$b class=\"value\" id=\"$k\">".nl2br(b7_text2html($d))."$u"; else $result .= ' '; // nothing to show, so do it the simple way break; case 'cent': $result .= "<$b class=\"value\" id=\"$k\">".b7_cent2text($d)."$u"; break; case 'euro': $result .= "<$b class=\"value\" id=\"$k\">".b7_euro2text($d)."$u"; break; //------------------------------------------------------------------- default: break; } } // append unit to input-fields if ( strlen($u) && in_array($v['type'], array('currency', 'date', 'text', 'password')) ) $result .= "$u"; // create link if ( isset ($v['href']) && ($d = b7_replace_ex($v['href'], $data)) ) $result = "$result"; return $result; } //--------------------------------------------------------------------------- function b7_form_create_inner($data, $form, $name='', $serial='') { $result = ''; if (isset($form)) { for (reset($form), $fieldset = FALSE, $fieldcls = '', $follow = FALSE, $text = ''; list($k, $v) = each($form); ) { $i = b7_form_field($k, $v, $name, $data, $follow, $serial); if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if (isset($v['fieldset'])) // change in fieldset { if ($fieldset !== FALSE) // end open fieldset $result .= b7_element_fieldset($text, $fieldset, $fieldcls); else $result .= $text; $text = ''; $fieldset = $v['fieldset'] === TRUE ? '' : $v['fieldset']; $fieldcls = isset($v['class']) ? $v['class'] : ''; } $c = !empty($v['class']) ? " class=\"{$v['class']}\"" : ''; // field class $t = b7_text2html(isset($v['title']) ? $v['title'] : ''); // field title if (strlen($t) || strlen($i)) // skip when no title and no value ('html' | 'show') { if ($follow) $follow = FALSE; else if (!empty($v['noclear'])) $text .= "\n"; else $text .= "
\n"; $text .= isset($v['title']) ? "\n" : ''; $text .= "$i\n"; if (!empty($v['follow'])) $follow = TRUE; else $text .= "
\n"; } else // keep follow flag $follow = !empty($v['follow']); } else if (strlen($i)) $text .= "$i\n"; } if ($follow) $text .= ""; if ($fieldset !== FALSE) $result .= b7_element_fieldset($text, $fieldset, $fieldcls); else $result .= $text; } return $result; } function b7_form_create($data, $form, $action='', $name='', $index=0, $id='id', $info=null) { $result = $index ? b7_element_hidden($id, $index) . "\n" : ""; $result .= $info ? b7_element_hidden(null, $info) . "\n" : ""; $result .= b7_form_create_inner($data, $form, $name); return b7_element_form($result, $action, $name); } function b7_form_show($data, $form) { for (reset($form); list($k, $v) = each($form); ) if (!empty($v['type'])) switch ($v['type']) { case 'button': case 'button_ex': case 'submit': case 'image': case 'hidden': case 'private': $form[$k]['type'] = ''; break; case 'static': case 'custom': case 'cent': case 'euro': case 'html': case 'show': break; default : $form[$k]['type'] = 'show'; break; } return b7_form_create_inner($data, $form); } function b7_form_validate(&$data, $form) { $result = array(); for (reset($form), $title = ''; list($k, $v) = each($form); ) { if (!isset($v['type']) || !in_array($v['type'], array('hidden','private'))) { if (isset($data[$k]) && isset($v['validate'])) $data[$k] = $v['validate']($data[$k]); if (isset($v['required']) && $v['required']) { if (!isset($data[$k]) || !strlen(trim($data[$k]))) { $title = empty($v['title']) ? $title : $v['title']; if (!in_array($title, $result)) array_push($result, $title); } } if (!empty($v['follow']) && !empty($v['title'])) $title = $v['title']; // combined field title } } return $result; } function b7_form_file(&$data, $form, $path = '/', $validate = '', $serial = '') // file upload handler { $result = ''; if (defined('DOCUMENT_ROOT')) $path = DOCUMENT_ROOT.$path; for (reset($form); list($k, $v) = each($form); ) { if (isset($v['type']) && $v['type'] == 'file' && isset($_FILES[$k])) { $file = $_FILES[$k]; if (strlen($serial)) { $file_error = $file['error' ][$serial]; $file_name = $file['name' ][$serial]; $file_tmp_name = $file['tmp_name'][$serial]; } else { $file_error = $file['error' ]; $file_name = $file['name' ]; $file_tmp_name = $file['tmp_name']; } if (!$file_error && strlen($file_name)) { if (!$validate || !($result = $validate($k, $file_tmp_name, $file_name))) { if (copy($file_tmp_name, $path.$file_name)) { if (($info = getimagesize($path.($data[$k] = $file_name))) && is_array($info)) { $data[$k."_x"] = $info[0]; $data[$k."_y"] = $info[1]; } } else $result .= "'$file_name' konnte nicht nach '$path' kopiert werden.\n"; } } } } return $result; } function b7_form_read(&$data, $form, $name, $index, $id='id') { $result = ""; if ($index) { $sql = b7_sql_select($form, $name, "$name.$id = '$index'"); if ($res = mysql_query($sql)) { if ($row = mysql_fetch_assoc($res)) { $data = $row; for (reset($form); list($k, $v) = each($form); ) { $type = isset($v['type']) ? $v['type'] : ''; switch ($type) { case 'currency': $data[$k] = b7_currency2text($data[$k]); break; case 'date': $data[$k] = b7_date2text($data[$k]); break; } } } // else // $result = 'not found'; mysql_free_result($res); } else $result = mysql_error(); } return $result; } function b7_form_write($data, $form, $name, $index=0, $id='id', $priority='', $index_default=0) { $result = ''; $sql_set = ''; if (!$index && $index_default) // manual index creation $sql_set = "$id = '$index_default'"; for (reset($form); list($k, $v) = each($form); ) { if ((!isset($v['type']) || !in_array($v['type'], array('static', 'private', 'button', 'button_ex', 'image', 'submit', 'result'))) && !isset($v['join']) && !($k == $id && !$index && $index_default)) // avoid double definition of index field { $type = isset($v['type']) ? $v['type'] : ''; if (isset($data[$k]) || $type == 'checkbox') // force data of checkboxes for clearing { $val = isset($data[$k]) ? $data[$k] : array(); // if no data, then we got empty checkboxes if (strlen($sql_set)) // add seperator $sql_set .= ",\n"; switch ($type) { case 'checkbox': // serialize the result of checkbox array $val = sizeof($val) ? b7_implode(',', $val) : (isset($v['default']) ? $v['default'] : ''); break; case 'currency': $val = b7_text2currency($val); break; case 'date': $val = b7_text2date($val); break; } $sql_set .= "$k = '$val'"; } } } if (strlen($sql_set)) { $sql = $index ? "UPDATE $name SET $sql_set WHERE $id = '$index' LIMIT 1" : "INSERT INTO $name SET $sql_set"; if (mysql_query($sql)) { if (!$index && $priority && ($index = $index_default ? $index_default : mysql_insert_id())) // add priority if (!mysql_query("UPDATE $name SET $priority = $index WHERE $id = '$index'")) $result = mysql_error(); } else $result = mysql_error(); } else $result = "no data"; return $result; } function b7_list_js_array($data, $name, $field='', $listfield='') //@@@ modified for lichtmacht { $result = ''; $result .= "\n"; return $result; } //--------------------------------------------------------------------------- function b7_list_cell_data($v, $v1, $k, $k2) { $result = isset($v['default']) ? $v['default'] : null; if (isset($v1[$k])) { $data = $v1[$k]; if ($k2 < 0) { $result = $data; } else if (is_array($data)) { if (isset($data[$k2])) $result = $data[$k2]; } else { if (sizeof($temp = b7_explode(',', $data)) > $k2) $result = $temp[$k2]; } } return $result; } function b7_list_cell_name($v, $name, $k, $k1, $k2) { if (strlen($name) && (empty($v['type']) || !in_array($v['type'], array('button', 'button_ex', 'image', 'submit')))) $result = $k2 < 0 ? "{$name}[$k1][$k]" : "{$name}[$k1][$k][$k2]"; else if (isset($v['type']) && $v['type'] == 'image') $result = $k2 < 0 ? "{$k}[$k1][]" : "{$k}[$k1][$k2][]"; else $result = $k2 < 0 ? "{$k}[$k1]" : "{$k}[$k1][$k2]"; return $result; } function b7_list_cell_unit($v) { return b7_form_field_unit($v); } function b7_list_cell($k, $v, $name, $k1, $v1, $k2) { $result = ''; $d = b7_list_cell_data($v, $v1, $k, $k2); // default|value $n = b7_list_cell_name($v, $name, $k, $k1, $k2); // variable name $u = b7_list_cell_unit($v); // unit extension $e = b7_event_handler($v); // event handler $i = $k2 < 0 ? "{$k}_{$k1}" : "{$k}_{$k1}_{$k2}"; // identifier $r = !empty($v['readonly']) ? ' readonly' : ''; // readonly // supress unit for output-fields when no data available if (!is_array($d) && !strlen($d) && !in_array($v['type'], array('currency', 'date', 'text', 'password'))) $u = ''; if (isset($v['type'])) { switch ($v['type']) { case 'button_ex': $result .= ""; break; case 'checkbox': if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_checkbox($i, $v['list'], $n, $d, $e); break; case 'radio': if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_radio($i, $v['list'], $n, $d, $e); break; case 'select': if (isset($v['list']) && is_array($v['list'])) $result .= b7_element_select($i, $v['list'], $n, $d, $e); break; case 'file': $result .= ""; $result .= ""; break; case 'textarea': $result .= ""; break; case 'date': case 'currency': $result .= ""; break; case 'text': case 'password': case 'submit': case 'button': $result .= ""; break; case 'image': $result .= ""; break; //----- output types ---------------------------------------------------- case 'hidden': case 'private': $result .= b7_element_hidden($n, $d); break; case 'static': case 'custom': if (isset($v['default'])) $d = b7_replace_ex($v['default'], $v1); $result .= $d.$u; break; case 'html': if (isset($v['list']) && is_array($v['list']) && isset($v['list'][$d])) $d = $v['list'][$d]; $result = nl2br($d).$u; break; case 'show': if (isset($v['list']) && is_array($v['list']) && isset($v['list'][$d])) $d = $v['list'][$d]; $result = nl2br(b7_text2html($d)).$u; break; case 'cent': $result .= ''.b7_cent2text($d)."$u"; break; case 'euro': $result .= ''.b7_euro2text($d)."$u"; break; //----- list only types ------------------------------------------------- case 'list': $result .= b7_text2html($d); break; case 'input': if ($k2 < 0 || isset($d)) // value must be defined for cell to be displayed $result .= ""; break; case 'result': $result .= " "; break; default: break; } } // append unit for input-fields if ( strlen($u) && in_array($v['type'], array('currency', 'date', 'text', 'password')) ) $result .= "$u"; // create link if ( isset ($v['href']) && ($d = b7_replace_ex($v['href'], $v1)) ) $result = "$result"; return $result; } function b7_list_create_header($list, $link) { $result = ''; // table header $result .= "\n"; for (reset($list); list($k, $v) = each($list); ) { if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if (isset($v['title']) && is_array($v['title'])) // matrix { for (reset($v['title']); list($k2, $v2) = each($v['title']); ) $result .= "$v2\n"; } else { $title = isset($v['title']) ? $v['title'] : ''; $order = isset($v['order']) ? $v['order'] : $k; if ($link && !in_array($v['type'], array('static', 'private', 'button', 'button_ex', 'image', 'submit', 'result'))) $result .= "$title\n"; else $result .= "$title\n"; } } } $result .= "\n"; return $result; } function b7_list_create_footer($list, $foot) { $result = ''; // table footer $result .= "\n"; for ($c = $col = 0, reset($list); list($k, $v) = each($list); ) { if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if (isset($foot[$k])) { $cl = isset($v['class']) ? " class=\"{$v['class']}\"" : ""; $id = " id=\"$k\""; if ($col > $c) $result .= " \n"; $c = $col; $result .= "{$foot[$k]}\n"; } else { if (isset($v['title']) && is_array($v['title'])) // matrix $col += sizeof($v['title']); else $col++; } } } if ($col > $c) $result .= " \n"; $result .= "\n"; return $result; } function b7_list_create_inner($data, $list, $group='', $link='', $name='', $foot=array()) { $result = ''; $size = 0; $oe = array('odd', 'even'); if (sizeof($data)) { $result .= "\n"; // table size for (reset($list); list($k, $v) = each($list); ) if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if (isset($v['title']) && is_array($v['title'])) // matrix $size += sizeof($v['title']); else $size++; } // table rows for ($g = '', $i = 0, reset($data); list($k1, $v1) = each($data); $i++) { // table header / group seperator if (strlen($group) && isset($v1[$group])) { if ($g != $v1[$group]) { $g = $v1[$group]; $k = $group; $v = $list[$group]; $v['type'] = 'show'; // force visibility $k2 = -1; $result .= "\n"; $result .= b7_list_create_header($list, $link); } } else { if ($i == 0) $result .= b7_list_create_header($list, $link); } // scan for rowspan - only one list item is allowed! for ($col = 0, $c = $size, $r = 1, reset($list); ($c == $size) && (list($k, $v) = each($list)); ) { if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if ($v['type'] == 'list') { $c = $col; // col to split $r = max(1, sizeof(b7_explode(',', $v1[$k]))); // rowspan } if (isset($v['title']) && is_array($v['title'])) $col += sizeof($v['title']); else $col++; } } // loop for rowspan for ($row = 0; $row < $r; $row++) { $result .= "\n"; //table columns for (reset($list), $col = 0; list($k, $v) = each($list); ) { if (!$row || $col >= $c) { // inquire rowspan $rs = (!$row && $col < $c && $r > 1) ? " rowspan=\"$r\"" : ""; // cell class $cl = isset($v['class']) ? " class=\"{$v['class']}\"" : ""; if (isset($v['title']) && is_array($v['title'])) { for (reset($v['title']); list($k2, $v2) = each($v['title']); ) { // build cell based on type $cell = b7_list_cell($k, $v, $name, $k1, $v1, k2); $result .= "$cell\n"; } } else { $k2 = $col >= $c ? $row : -1; // multidimensional ? if (!empty($v['type'])) { // build cell based on type $cell = b7_list_cell($k, $v, $name, $k1, $v1, $k2); if (!in_array($v['type'], array('hidden', 'private'))) $result .= "$cell\n"; else $result .= $cell . "\n"; } } } // count visible rows if (!empty($v['type']) && !in_array($v['type'], array('hidden', 'private'))) { if (isset($v['title']) && is_array($v['title'])) // matrix $col += sizeof($v['title']); else $col++; } } $result .= "\n"; } } // result row - only if at least one result-field for (reset($list); list($k, $v) = each($list); ) if (isset($v['type']) && $v['type'] == 'result') if (empty($foot[$k])) $foot[$k] = ' '; if (sizeof($foot)) $result .= b7_list_create_footer($list, $foot); $result .= "
".b7_list_cell($k, $v, $name, $k1, $v1, $k2)."
\n"; } return $result; } function b7_list_create($data, $list, $group='', $link='', $action='', $name='', $form=null, $info=null) { $result = b7_list_create_inner($data, $list, $group, $link, $name); $result .= b7_element_hidden(null, $info) . "\n"; $result .= b7_form_create_inner(null, $form, $name); // form elements, no data - i.e. buttons for submitting only return b7_element_form($result, $action, $name); } function b7_list_show($data, $list, $group='', $link='', $foot=array()) { for (reset($list); list($k, $v) = each($list); ) if (!empty($v['type'])) switch ($v['type']) { case 'button': case 'button_ex': case 'submit': case 'image': case 'hidden': case 'private': $list[$k]['type'] = ''; break; case 'static': case 'custom': case 'cent': case 'euro': case 'list': case 'html': case 'show': break; default : $list[$k]['type'] = 'show'; break; } return b7_list_create_inner($data, $list, $group, $link, '', $foot); } function b7_list_read(&$data, $list, $name, $order='', $where='', $id='', $sub_id='', $limit='') { $error = ""; $sql = b7_sql_select($list, $name, $where, $order, $limit); if ($res = mysql_query($sql)) { for ($i = 0; $row = mysql_fetch_assoc($res); $i++) { $j = ($id && isset($row[$id])) ? $row[$id] : $i; if (isset($data[$j]) || !empty($sub_id)) while (list($k, $v) = each($row)) if ($k == $sub_id) $data[$j][$k][$v] = ''; else $data[$j][$k] = $v; else $data[$j] = $row; for (reset($list); list($k2, $v2) = each($list); ) { $type = isset($v2['type']) ? $v2['type'] : ''; switch ($type) { case 'currency': $data[$j][$k2] = b7_currency2text($data[$j][$k2]); break; case 'date': $data[$j][$k2] = b7_date2text($data[$j][$k2]); break; } } } mysql_free_result($res); } else $error = mysql_error(); return $error; } function b7_list_write($data, $list, $name, $index=0, $id='', $sub_id='') { $error = ''; if (!empty($id) && !isset($list[$id])) $list[$id] = array(); // append for storing index for (reset($data); !$error && (list($k, $v) = each($data)); ) { if (!empty($id)) $v[$id] = $index; if (!empty($sub_id) && isset($v[$sub_id])) $error = b7_form_write($v, $list, $name, $v[$sub_id], $sub_id); else $error = b7_form_write($v, $list, $name); } return $error; } //--------------------------------------------------------------------------- function b7_matrix_read(&$data, $id, $id2, $val, $name, $keys=null, $zerofix=TRUE, $indirect=TRUE, $insert=TRUE) { $error = ''; $sql = " SELECT $id, $id2, $val FROM $name "; if (is_array($keys)) { for (reset($keys), $where = '1'; list($k, $v) = each($keys); ) $where .= "\nAND $k = '$v'"; $sql .= "\nWHERE $where"; } if ($res = mysql_query($sql)) { while ($row = mysql_fetch_assoc($res)) { $k = $row[$id]; $k2 = $row[$id2] == '' && $zerofix ? '0' : $row[$id2]; if ($insert || isset($data[$k])) { if ($indirect) $data[$k][$val][$k2] = $row[$val]; else $data[$k][$k2] = $row[$val]; } } mysql_free_result($res); } else $error = mysql_error(); return $error; } function b7_matrix_write($data, $id, $id2, $val, $name, $keys=null, $zerofix=TRUE, $indirect=TRUE) { $temp = array(); $set = ''; $where = '1'; $error = b7_matrix_read(&$temp, $id, $id2, $val, $name, $keys, $zerofix, $indirect); if (is_array($keys)) for (reset($keys); list($k, $v) = each($keys); ) { $set .= "\n $k = '$v',"; $where .= "\nAND $k = '$v' "; } for (reset($data); !$error && (list($k, $v) = each($data)); ) if ($indirect) { if (isset($v[$val])) { while (!$error && (list($k2, $v2) = each($v[$val]))) { if ($k2 == '0' && $zerofix) // index '' is transalted to '0' $k2 = ''; if (!isset($temp[$k][$val][$k2])) // does not exist, so insert $sql = " INSERT INTO $name SET $set $id = '$k', $id2 = '$k2', $val = '$v2' "; else { if ($temp[$k][$val][$k2] != $v2) // other value, so update $sql = " UPDATE $name SET $val = '$v2' WHERE $where AND $id = '$k' AND $id2 = '$k2' "; else // same value, do nothing $sql = ''; unset($temp[$k][$val][$k2]); } if ($sql) if (!mysql_query($sql)) $error = mysql_error(); } } } else { while (!$error && (list($k2, $v2) = each($v))) { if ($k2 == '0' && $zerofix) // index '' is transalted to '0' $k2 = ''; if (!isset($temp[$k][$k2])) // does not exist, so insert $sql = " INSERT INTO $name SET $set $id = '$k', $id2 = '$k2', $val = '$v2' "; else { if ($temp[$k][$k2] != $v2) // other value, so update $sql = " UPDATE $name SET $val = '$v2' WHERE $where AND $id = '$k' AND $id2 = '$k2' "; else // same value, do nothing $sql = ''; unset($temp[$k][$k2]); } if ($sql) if (!mysql_query($sql)) $error = mysql_error(); } } for (reset($temp); !$error && (list($k, $v) = each($temp)); ) // delete the rest while (!$error && (list($k2, $v2) = ($indirect ? each($v[$val]) : each($v)))) { $sql = " DELETE FROM $name WHERE $where AND $id = '$k' AND $id2 = '$k2' "; if (!mysql_query($sql)) $error = mysql_error(); } return $error; } function b7_array_swap($input, $cols) { $result = array(); $keys = array(); $size = sizeof($input); $rows = intval(($size - 1) / $cols) + 1; $rest = $size % $cols; for (reset($input), $r = $c = 0; list($k) = each($input); ) { $keys[$r][$c] = $k; if (++$r >= $rows || ($rest && $c >= $rest && $r >= $rows - 1)) { $r = 0; $c++; } } for ($r = 0, $i = 0; $r < $rows; $r++) for ($c = 0; $c < $cols && $i < $size; $c++, $i++) $result[$k = $keys[$r][$c]] = $input[$k]; return $result; } ?> "; else $alternative = ""; } else $alternative = ""; if ( strlen($flash_path) ) { if ( $flash_params != null ) { if ( is_array($flash_params) ) $arr_flash_params = $flash_params; else if ( sizeof($arr1 = preg_split("/[\s,]+/", $flash_params)) ) { for ( $arr_flash_params = array(); list($k, $v) = each($arr1); ) if ( sizeof($arr2 = preg_split("/=/", $v)) >= 2 ) $arr_flash_params[$arr2[0]] = $arr2[1]; } else $arr_flash_params = array(); } else $arr_flash_params = array(); $output.= "\n"; $output.= ""; } else $output = $alternative; return $output; } function b7_multi_like($key, $exp, $not = '') { $arr = is_array($exp) ? $exp : preg_split("/[\s,]+/", $exp); for (reset($arr); list($k, $v) = each($arr); ) $arr[$k] = "$key $not LIKE '%$v%'"; return sizeof($arr) ? implode(' AND ', $arr) : '1'; } function b7_multi_in($key, $exp, $not = '') { $arr = is_array($exp) ? $exp : preg_split("/[\s,]+/", $exp); return sizeof($arr) ? "$key $not IN ('".implode("','", $arr)."')" : '1'; } //===== external functions ================================================== function b7_banner($keywords = '', $width = 0, $height = 0) // database must be selected { static $exclude = array(); $id = 0; $image_path = ''; $click_link = ''; $flash_path = ''; $flash_params = ''; $sql = " SELECT id, width, height, flash_name, flash_version, flash_params, image_name, text_name, click_link FROM banner WHERE status = '1' AND (activate_date = '0000-00-00' OR activate_date <= NOW()) AND (expire_date = '0000-00-00' OR expire_date >= NOW()) AND ".(intval($width) ? "width=$width" : '1')." AND ".(intval($height) ? "height=$height" : '1')." AND ".b7_multi_like('keywords', $keywords)." AND ".b7_multi_in('id', $exclude, 'NOT')." ORDER BY RAND() LIMIT 1 "; if ( $res = mysql_query($sql) ) { if ( $row = mysql_fetch_assoc($res) ) { $id = $row['id']; $width = $row['width']; $height = $row['height']; $image_path = strlen($row['image_name']) ? BANNER_PATH.$row['image_name'] : (strlen($row['text_name']) ? $row['text_name'] : ''); $click_link = $row['click_link']; $flash_path = strlen($row['flash_name']) ? BANNER_PATH.$row['flash_name'] : ''; $flash_version = $row['flash_version']; $flash_params = $row['flash_params']; array_push($exclude, $id); // add to exclusion-list /*----- not used due to old version of mysql-server ------------------------- $sql2 = " SELECT id, view_count FROM banner_view WHERE banner_id = $id AND view_date = DATE(NOW()) "; ---------------------------------------------------------------------------*/ $sql2 = " SELECT id, view_count FROM banner_view WHERE banner_id = $id AND view_date = LEFT(NOW(),10) "; if ( $res2 = mysql_query($sql2) ) { if ( $row2 = mysql_fetch_assoc($res2) ) { $sql3 = " UPDATE banner_view SET view_count = ".($row2['view_count'] + 1)." WHERE id = {$row2['id']} LIMIT 1 "; } else { /*----- not user due to old version of mysql-server ------------------------- $sql3 = " INSERT INTO banner_view SET banner_id = $id, view_date = DATE(NOW()), view_count = 1 "; ---------------------------------------------------------------------------*/ $sql3 = " INSERT INTO banner_view SET banner_id = $id, view_date = LEFT(NOW(),10), view_count = 1 "; } mysql_query($sql3); mysql_free_result($res2); } } mysql_free_result($res); } if ( $id ) { if ( BANNER_CLICK ) $click_link = BANNER_CLICK.'?id='.$id.'&referer='.$_SERVER['PHP_SELF']; $code = b7_show_banner($width, $height, $image_path, $click_link, $flash_path, $flash_params, $flash_version); } else $code = ''; return $code; } function b7_banner_count($keywords = '', $width = 0, $height = 0) // database must be selected { $cnt = 0; $sql = " SELECT count(id) FROM banner WHERE status = '1' AND (activate_date = '0000-00-00' OR activate_date <= NOW()) AND (expire_date = '0000-00-00' OR expire_date >= NOW()) AND ".(intval($width) ? "width=$width" : '1')." AND ".(intval($height) ? "height=$height" : '1')." AND ".b7_multi_like('keywords', $keywords)." "; if ( $res = mysql_query($sql) ) { $cnt = mysql_result($res, 0); mysql_free_result($res); } return $cnt; } ?>