Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* this is the prototype */
- require_once("htmlparser.inc");
- function attributesToString($attributes)
- {
- $string = "";
- foreach($attributes as $key => $value)
- {
- $string .= $key.'="'.$value.'" ';
- }
- return $string;
- }
- function substrHTML_parser( & $parser, $maxLength, & $count )
- {
- $html = "";
- while ( $parser->parse() )
- {
- if( $parser->iNodeType === NODE_TYPE_TEXT )
- {
- $valueLength = strlen($parser->iNodeValue);
- if($valueLength > 0)
- {
- if($count < $maxLength)
- {
- if( ($valueLength + $count) > $maxLength )
- {
- $html .= substr( $parser->iNodeValue, 0, ($maxLength - $count + 1) )."...";
- }
- else
- {
- $html .= $parser->iNodeValue;
- }
- }
- $count += $valueLength;
- }
- }
- else if( $parser->iNodeType === NODE_TYPE_ELEMENT )
- {
- $html .= "<".$parser->iNodeName." ".attributesToString( $parser->iNodeAttributes ).">";
- $html .= substrHTML_parser( $parser, $maxLength, $count );
- }
- else if( $parser->iNodeType === NODE_TYPE_ENDELEMENT )
- {
- $html .= "</".$parser->iNodeName.">";
- return $html;
- }
- }
- return $html;
- }
- function substrHTML( & $html, $maxLength )
- {
- $parser = new HtmlParser($html);
- $html = substrHTML_parser($parser, $maxLength, $count = 0);
- if($count <= $maxLength)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- class dynamic
- {
- var $mysql_link;
- var $admin;
- var $newButtons = '<span class="buttons"><img class="add" src="/admin/images/plus.png" title="add"><img class="clear" src="/admin/images/clear.png" title="reset form"></span>';
- var $editButtons = '<span class="buttons"><img class="move" src="/admin/images/vertical.png" title="drag to change order"><img class="delete" src="/admin/images/delete.png" title="double click to delete"><img src="/admin/images/pencil.png" class="edit" title="edit"></span>';
- var $updateButtons = '<span class="buttons"><img class="move" src="/admin/images/vertical.png" title="drag to change order"><img class="update" src="/admin/images/check.png" title="save"><img class="get" src="/admin/images/undo.png" title="undo / revert"></span>';
- var $tables = array
- (
- "tradeshows",
- "FAQ",
- "inTheMedia",
- "ourPeople",
- "general",
- "sales"
- );
- var $fields = array
- (
- array
- (
- "boothInfo",
- "link",
- "location",
- "title",
- "tradeshowDate"
- ),
- array
- (
- "answer",
- "category",
- "question"
- ),
- array
- (
- "title",
- "dateOf",
- "sourceTitle",
- "category",
- "body",
- "link",
- "pictureSmall",
- "pictureBig"
- ),
- array
- (
- "title",
- "subTitle",
- "body",
- "picture"
- ),
- array
- (
- "title",
- "subTitle",
- "body",
- "category"
- ),
- array
- (
- "title",
- "body"
- )
- );
- var $categories = array
- (
- false,
- array('About', 'About HDPE', 'About Applicators'),
- array('Press Releases', 'Articles', 'In The News'),
- false,
- array('What We Do', 'Keen On Green', 'Sales', 'Customer Service', 'Applicator Service', 'Contact', 'Our People'),
- false
- );
- var $templates = array
- (
- "tradeshows",
- "FAQ",
- "inTheMedia/sidebar/press releases",
- "inTheMedia",
- "inTheMedia/sidebar/Articles",
- "inTheMedia/sidebar/In The News",
- "index/In The News",
- "In The News",
- "In The News/item",
- "general",
- "ourPeople",
- "ourPeople/top",
- "ourPeople/bottom",
- "ourPeople/sidebar",
- "sales"
- );
- var $rootDir;
- var $templateDir;
- var $users = array('username' => 'password');
- var $salt = "XXXXX:!:@;XXXX:XX:";
- function construct()
- {
- $this->mysql_link = mysql_connect("localhost", "mysqlusername", "mysqlpassword");
- mysql_select_db("the_db", $this->mysql_link);
- $this->rootDir = $_SERVER['DOCUMENT_ROOT']."/admin/";
- // $this->rootDir = dirname(__FILE__)."/admin/";
- $this->templateDir = $this->rootDir."templates/";
- $this->isAdmin();
- }
- function isAdmin()
- {
- if (isset($_COOKIE["admin"]) && $_COOKIE["admin"] === sha1("username".$this->salt.$this->users["username"]))
- {
- $this->admin = true;
- return true;
- }
- else
- {
- $this->admin = false;
- return false;
- }
- }
- function login()
- {
- if($this->isAdmin())
- {
- return true;
- }
- if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($this->users[$_SERVER['PHP_AUTH_USER']]) || $this->users[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'])
- {
- header('WWW-Authenticate: Basic realm="Administration"');
- header('HTTP/1.0 401 Unauthorized');
- echo 'Authorization Required';
- exit;
- }
- else
- {
- setcookie("admin", sha1("username".$this->salt.$this->users["username"]), 0, "/", ".sitename.com");
- $this->admin = true;
- return true;
- }
- }
- function logout()
- {
- header('HTTP/1.0 401 Unauthorized');
- setcookie("admin", "", time() - 1, "/", ".sitename.com");
- }
- function get($templates, $template_modifier, $table, $id = false, $category = false, $array = false)
- {
- if($id)
- {
- $sqlWhere = "`id`=".$id."";
- }
- else if($category !== false)
- {
- $sqlWhere = "`category`='".$this->categories[$table][$category]."'";
- if($array)
- {
- foreach($array as $value)
- {
- $sqlWhere .= " AND `" .$value[0]. "` ".$value[1]." '" .mysql_real_escape_string($value[2]). "'";
- }
- }
- }
- else
- {
- $sqlWhere = "";
- }
- $result = mysql_query("SELECT `id`,`".implode("`,`", $this->fields[$table])."` FROM `".$this->tables[$table]."`".(empty($sqlWhere) ? "" : "WHERE ".$sqlWhere)." ORDER BY sort ASC");
- if($this->admin)
- {
- $buttons = $this->editButtons;
- }
- else
- {
- $butons = "";
- }
- $return = array();
- //$keepByTemplate = array();
- $keep = array();
- while ($item = mysql_fetch_assoc($result))
- {
- $id = $item['id'];
- $item["buttons"] = $buttons;
- foreach($templates as $template)
- {
- //$keep & = $keepByTemplate[$template];
- $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
- $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
- $return[$template] .= include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
- }
- }
- return $return;
- }
- function edit($template, $template_modifier, $table, $id = false, $category = false)
- {
- if($id)
- {
- $result = mysql_query("SELECT `id`,`".implode("`,`", $this->fields[$table])."` FROM ".$this->tables[$table]." WHERE id='".$id."' ORDER BY sort ASC");
- $item = mysql_fetch_assoc($result);
- $item["selectHidden"] = '<input type="hidden" name="category" value="'.$item["category"].'">';
- $item["class"] = "dynamic item edit id".$item['id']." te".$template." ta".$table." ca".$category;
- $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
- $item["buttons"] = $this->updateButtons;
- }
- else
- {
- $item = array();
- $item["selectHidden"] = '<input type="hidden" name="category" value="'.$category.'">';
- $item["id"] = "id-".$template."-".$table."-".$category."-new";
- $item["buttons"] = $this->newButtons;
- $item["class"] = "dynamic item new";
- }
- if($category !== false)
- {
- $item["required"] = '<input type="hidden" name="category" value="'.$category.'">';
- }
- else if($this->categories[$table])
- {
- $item["required"] = '<select name="category">';
- foreach($this->categories[$table] as $value => $name)
- {
- $item["required"] .= '<option value="'.$value.'"'.(isset($item["category"]) && $item["category"] == $name ? 'selected="selected"' : '').'>'.$name.'</option>';
- }
- $item["required"] .= '</select>';
- }
- return include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
- }
- function script()
- {
- if(!$this->admin)
- {
- return '
- <link type="text/css" href="/admin/css/overcast/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
- <script type="text/javascript" src="/admin/js/jquery-ui-1.8.11.custom.min.js"></script>
- ';
- }
- return '
- <script type="text/javascript">
- _editor_url = "/admin/xinha/" // (preferably absolute) URL (including trailing slash) where Xinha is installed
- _editor_lang = "en"; // And the language we need to use in the editor.
- _editor_skin = "blue-look"; // If you want use a skin, add the name (of the folder) here
- _editor_icons = "Crystal"; // If you want to use a different iconset, add the name (of the folder, under the `iconsets` folder) here
- </script>
- <script type="text/javascript" src="/admin/xinha/XinhaCore.js"></script>
- <script type="text/javascript" src="/admin/js/xinha.php"></script>
- <script>
- $(function()
- {
- dataOfPages = {};
- $( ".sortable" ).sortable(
- {
- items: ".edit",
- update: function(event, ui)
- {
- var $ul = $(this);
- $.post("/admin/ajax.php", { action: "sort", order: $ul.sortable("toArray") },
- function(data)
- {
- });
- }
- })
- function buttons()
- {
- var $this = $(this);
- switch ($this.attr("class"))
- {
- case "delete":
- if($this.css("opacity") == 1)
- {
- var info = $this.closest(".dynamic.item").attr("id").split("-");
- var $dynamic = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
- $.post("/admin/ajax.php", { action: "delete", table: info[2], id: info[4] },
- function(data)
- {
- if(window["editableCallback"])
- {
- window["editableCallback"]("delete", $dynamic);
- }
- $dynamic.remove();
- });
- }
- else
- {
- $this.fadeTo("fast", 1);
- }
- break;
- case "get":
- var info = $this.closest(".dynamic.item").attr("id").split("-");
- var $items = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
- var templates = $items
- .map(function()
- {
- return (this.id.split("-"))[1];
- })
- .get();
- $.post("/admin/ajax.php", { action: "get", templates: templates, table: info[2], id: info[4] },
- function(data)
- {
- for(template in data)
- {
- $items.filter(".te"+template)
- .html(data[template])
- .find(".buttons img")
- .click(buttons);
- }
- if(window["editableCallback"])
- {
- window["editableCallback"]("get", $items);
- }
- });
- break;
- case "edit":
- var $item = $this.closest(".dynamic.item");
- var info = $item.attr("id").split("-");
- $.post("/admin/ajax.php", { action: "edit", template: info[1], table: info[2], category: info[3], id: info[4] },
- function(data)
- {
- $item
- .html(data)
- .find(".buttons img")
- .click(buttons);
- if(xinha_init)
- {
- xinha_init();
- }
- $( ".datepicker" )
- .datepicker(
- {
- dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, showButtonPanel: true,
- onClose: function(dateText, inst) {
- var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
- var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
- $(this).datepicker("setDate", new Date(year, month, 1));
- },
- beforeShow : function(input, inst) {
- if ((datestr = $(this).val()).length > 0) {
- year = datestr.substring(datestr.length-4, datestr.length);
- month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker("option", "monthNames"));
- $(this).datepicker("option", "defaultDate", new Date(year, month, 1));
- $(this).datepicker("setDate", new Date(year, month, 1));
- }
- }
- })
- .keydown(function() { return false; });
- if(window["editableCallback"])
- {
- window["editableCallback"]("edit", $item);
- }
- });
- break;
- case "update":
- var $item = $this.closest(".dynamic.item");
- var $form = $item.children("form");
- $form.submit(function(){return false;}).submit();
- var category = "";
- var o = {};
- $.map($form.find("[name]"), function(n, i)
- {
- if("formatblock fontname fontsize listtype".indexOf(n.name) == -1)
- {
- o[n.name] = $(n).val();
- if(n.name == "category")
- {
- category = $(n).val();
- }
- }
- });
- var info = $item.attr("id").split("-");
- var $items = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
- var $containers = $(".dynamic.container.ta"+info[2]+".id"+info[4]);
- var templates = $items//.add($containers)
- .map(function()
- {
- return (this.id.split("-"))[1];
- })
- .get();
- $.post("/admin/ajax.php", { action: "update", templates: templates, table: info[2], id: info[4], data: o },
- function(data)
- {
- window.location.reload();
- /*
- if(data.category && $containers.length == 0)
- {
- $items.remove();
- console.log(data);
- for(template in data.data)
- {
- $container = $containers.filter(".te"+template)
- .append(data.data[template]);
- $container
- .filter(".sortable")
- .sortable( "refresh" );
- $container
- .find(".dynamic.item:last .buttons img")
- .click(buttons)
- .filter(".delete")
- .mouseleave(deleteButtonReset);
- }
- }
- else
- {
- for(template in data.data)
- {
- $items.filter(".te"+template)
- .html(data.data[template])
- .find(".buttons img")
- .click(buttons);
- }
- }
- */
- if(window["editableCallback"])
- {
- window["editableCallback"]("update", $items);
- }
- });
- break;
- case "add":
- var $item = $this.closest(".dynamic.item");
- var $form = $item.children("form");
- $form.submit(function(){return false;}).submit();
- var category = "";
- var o = {};
- $.map($form.find("[name]"), function(n, i)
- {
- if("formatblock fontname fontsize listtype".indexOf(n.name) == -1)
- {
- o[n.name] = $(n).val();
- if(n.name == "category")
- {
- category = $(n).val();
- }
- }
- });
- var info = $item.attr("id").split("-");
- var $items = $(".dynamic.item.new, .dynamic.item.ta"+info[2]);
- var templates = $items
- .map(function()
- {
- return (this.id.split("-"))[1];
- })
- .get();
- $.post("/admin/ajax.php", { action: "add", templates: templates, table: info[2], data: o },
- function(data)
- {
- window.location.reload();
- /*
- if(!data)
- {
- return false;
- }
- var $container;
- for(template in data)
- {
- $container = $items.filter(".te"+template)
- .append(data[template]);
- $container
- .filter(".sortable")
- .sortable( "refresh" );
- $container
- .find(".dynamic.item:last .buttons img")
- .click(buttons)
- .filter(".delete")
- .mouseleave(deleteButtonReset);
- }
- $form[0].reset();
- if(window["editableCallback"])
- {
- window["editableCallback"]("update", $items);
- }
- */
- });
- break;
- case "clear":
- $this.closest("form")[0].reset();
- break;
- default:
- }
- }
- function deleteButtonReset()
- {
- $(this).fadeTo("fast", 0.5);
- }
- $(".dynamic.item .buttons img")
- .click(buttons)
- .filter(".delete")
- .mouseleave(deleteButtonReset);
- $( ".datepicker" )
- .datepicker(
- {
- dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, showButtonPanel: true,
- onClose: function(dateText, inst) {
- var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
- var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
- $(this).datepicker("setDate", new Date(year, month, 1));
- },
- beforeShow : function(input, inst) {
- if ((datestr = $(this).val()).length > 0) {
- year = datestr.substring(datestr.length-4, datestr.length);
- month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker("option", "monthNames"));
- $(this).datepicker("option", "defaultDate", new Date(year, month, 1));
- $(this).datepicker("setDate", new Date(year, month, 1));
- }
- }
- })
- .keydown(function() { return false; });
- });
- </script>
- <link type="text/css" href="/admin/css/overcast/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
- <script type="text/javascript" src="/admin/js/jquery-ui-1.8.11.custom.min.js"></script>
- ';
- }
- }
- class page extends dynamic
- {
- var $table;
- var $category;
- var $id;
- var $template;
- var $array;
- function page($template, $table, $category = false, $id = false)
- {
- if(isset($this->tables[$table]) && isset($this->templates[$template]))
- {
- $this->construct();
- $this->template = $template;
- $this->table = $table;
- $this->category = $category;
- if(!$id)
- {
- $this->array = $this->id = false;
- }
- else if(is_numeric($id))
- {
- $this->id = $id;
- $this->array = false;
- }
- else if(is_array($id))
- {
- $this->id = false;
- $this->array = $id;
- }
- else
- {
- die;
- }
- }
- else
- {
- die;
- }
- }
- function get()
- {
- $content = dynamic::get(array($this->template), "item", $this->table, $this->id, $this->category, $this->array);
- $content = $content[$this->template];
- $classAttr = "dynamic container te".$this->template." ta".$this->table." ca".$this->category." id".$this->id;
- $idAttr = "cid-".$this->template."-".$this->table."-".$this->category."-".$this->id;
- return include($this->templateDir.$this->templates[$this->template]."/container.php");
- }
- function get_input()
- {
- if($this->admin)
- {
- return $this->edit($this->template, "input", $this->table, $this->id, $this->category);
- }
- }
- }
- include("JSON.php");
- class ajax extends dynamic
- {
- function ajax()
- {
- $this->construct();
- }
- function isValidTemplates($templates)
- {
- if( is_array($templates) && !empty($templates) )
- {
- foreach($templates as $template)
- {
- if(!isset($this->templates[$template]))
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- function get($templates, $table, $id)
- {
- if(isset($this->tables[$table]) && $this->isValidTemplates($templates) && is_numeric($id))
- {
- return json_encode(dynamic::get($templates, "update", $table, $id));
- }
- }
- function update($templates, $table, $id, $item)
- {
- if(is_array($item) && isset($this->tables[$table]) && $this->isValidTemplates($templates) && is_numeric($id))
- {
- $fields = array_keys($item);
- $category_changed = false;
- if(array_diff($fields, $this->fields[$table]) == array())
- {
- $sql = array();
- $template_modifier = "update";
- if(isset($item["category"]) && isset($this->categories[$table][$item["category"]]))
- {
- $category = $item["category"];
- $item["category"] = $this->categories[$table][$item["category"]];
- $result = mysql_query("SELECT `category` FROM ".$this->tables[$table]." WHERE `id`=".$id." AND `category`='".$item["category"]."'");
- if(!mysql_num_rows($result))
- {
- $sqlWhere = " WHERE category='".$item["category"]."'";
- $result = mysql_query("SELECT MAX(`sort`) AS `sort` FROM ".$this->tables[$table].$sqlWhere);
- if($itemx = mysql_fetch_assoc($result))
- {
- $sql[] = "`sort`=".($itemx["sort"]+1);
- }
- else
- {
- $sql[] = "`sort`=0";
- }
- $template_modifier = "item";
- }
- }
- else
- {
- $category = "";
- }
- foreach($item as $field => $value)
- {
- $sql[] = "`".$field."`='".mysql_real_escape_string($value)."'";
- }
- if(mysql_query("UPDATE ".$this->tables[$table]." SET ".implode(",", $sql)." WHERE `id`=".$id))
- {
- $item["buttons"] = $this->editButtons;
- $return = array();
- foreach($templates as $template)
- {
- $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
- $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
- $item["buttons"] = $this->editButtons;
- $return[$template] .= include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
- }
- return json_encode(array("category" => $category_changed, "data" => $return));
- }
- }
- }
- }
- function edit($template, $table, $id)
- {
- if(isset($this->tables[$table]) && isset($this->templates[$template]) && is_numeric($id))
- {
- return dynamic::edit($template, "edit", $table, $id);
- }
- }
- function add($templates, $table, $item)
- {
- if(is_array($item) && isset($this->tables[$table]) && $this->isValidTemplates($templates))
- {
- $fields = array_keys($item);
- if(array_diff($fields, $this->fields[$table]) == array())
- {
- if(isset($item["category"]) && isset($this->categories[$table][$item["category"]]))
- {
- $category = $item["category"];
- $item["category"] = $this->categories[$table][$item["category"]];
- $sqlWhere = " WHERE category='".$item["category"]."'";
- }
- else
- {
- $category = "";
- $sqlWhere = "";
- }
- $result = mysql_query("SELECT MAX(`sort`) AS `sort` FROM ".$this->tables[$table].$sqlWhere);
- if($itemx = mysql_fetch_assoc($result))
- {
- $sort = $itemx["sort"]+1;
- }
- else
- {
- $sort = 0;
- }
- if(mysql_query("INSERT INTO ".$this->tables[$table]." (`".implode("`,`", $fields)."`, `sort`) VALUES ('".implode("','", array_map("mysql_real_escape_string", $item))."', ".$sort.")"))
- {
- $id = mysql_insert_id();
- $return = array();
- foreach($templates as $template)
- {
- $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
- $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
- $item["buttons"] = $this->editButtons;
- $return[$template] .= include($this->templateDir.$this->templates[$template]."/item.php");
- }
- return json_encode($return);
- }
- }
- }
- }
- function sort($order)
- {
- /* not optimized */
- if(is_array($order))
- {
- foreach($order as $position => $id)
- {
- $id = explode("-", $id);
- if(is_numeric($position) && is_numeric($id[4]) && isset($this->tables[$id[2]]))
- {
- if(mysql_query("UPDATE ".$this->tables[$id[2]]." SET sort=".$position." WHERE id=".$id[4]))
- {
- }
- else
- {
- }
- }
- }
- }
- }
- function delete($table, $id)
- {
- if(isset($this->tables[$table]) && is_numeric($id))
- {
- mysql_query("DELETE FROM ".$this->tables[$table]." WHERE id=".$id);
- }
- }
- function get_dates($table, $category = false)
- {
- $sqlWhere = "`category`='".$this->categories[$table][$category]."'";
- $result = mysql_query("SELECT `dateOf` FROM `".$this->tables[$table]."`".(empty($sqlWhere) ? "" : "WHERE ".$sqlWhere)." ORDER BY dateOf DESC");
- $return = "";
- $years = array();
- while ($item = mysql_fetch_assoc($result))
- {
- $date = explode("-", $item["dateOf"]);
- if(!in_array($date[0], $years))
- {
- $return .= '<li><a href="?year='.$date[0].'">'.$date[0].'</a></li>';
- $years[] = $date[0];
- }
- }
- return $return;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement