/** * default action */ function action($item) { //! create a fake page parameter $name = "layoutadd"; $_SESSION['cms_param'][sha1("layoutadd_")] = new \PHPPE\AddOn\layoutadd([], $name, $name); //! if layout not given if (empty($item)) { //! check if we have to activate a sitebuild if (!empty($_REQUEST['set'])) { DS::exec("UPDATE " . Views::$_table . " SET id=sitebuild WHERE sitebuild!='' AND id='frame'"); DS::exec("UPDATE " . Views::$_table . " SET id='frame' WHERE sitebuild=?", trim($_REQUEST['set'])); Http::redirect(); } //! load layouts and sitebuilds $this->layouts = Views::find([], "sitebuild=''", "name"); $this->sitebuilds = Views::find([], "sitebuild!=''", "name"); } else { //! load layout $this->layout = new Views($item); if (!empty($this->layout->jslib)) { foreach ($this->layout->jslib as $j) { View::jslib($j); } } if (!empty($this->layout->css)) { foreach ($this->layout->css as $c) { View::css($c); } } $this->numPages = Page::getNum($item); //! get user input $layout = Core::req2arr("layout"); //! merge the new data with the loaded layout's properties if (!empty($this->layout->sitebuild) && !empty($layout)) { Core::$core->noframe = 1; $layout['sitebuild'] = $layout['id']; } if (Core::isTry("layout")) { //! delete a layout if (!empty($layout['delete'])) { $this->layout->delete(); } else { unset($layout['delete']); //! renamed? if ($this->layout->id != $layout['id']) { DS::exec("UPDATE " . Views::$_table . " SET id=? WHERE id=?", [$layout['id'], $this->layout->id]); Core::log('A', sprintf("Layout %s renamed to %s by %s", $this->layout->id, $layout['id'], Core::$user->name), "cmsaudit"); } //! save new data foreach ($layout as $k => $v) { $this->layout->{$k} = $v; } $this->layout->save(); } Http::redirect("cms/layouts"); } } }
function edit() { View::jslib("wyswyg.js", "pe.wyswyg.init();", 8); View::css("wyswyg.css"); if (empty($this->args[0])) { $this->args[0] = $this->adjust; } return "<textarea id='" . $this->fld . "' name='" . $this->fld . "' class='" . $this->css . " wyswyg' dir='ltr' data-conf='" . htmlspecialchars(urlencode(json_encode($this->args))) . "'>" . htmlspecialchars($this->value) . "</textarea>"; }
function __construct() { //! common check for all action handlers if (!Core::$user->has("install")) { Http::redirect("login", true); } //! if bootstrap extension not installed, use cdn version if (!Core::isInst("bootstrap")) { View::css("http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"); } View::css("extensions.css"); View::jslib("sha256.js"); View::jslib("extensions.js"); }
public function testHelpers() { \PHPPE\Core::$core->nocache = true; \PHPPE\Core::$core->meta["test"] = "testing"; \PHPPE\Core::$core->link["apple-touch-icon"] = "testing"; \PHPPE\View::init([]); $dir = dirname(__DIR__); \PHPPE\View::setPath($dir); \PHPPE\View::assign("dir", $dir); \PHPPE\View::css("test.css"); \PHPPE\View::css("test2.css"); \PHPPE\View::css(url("css", "test.css")); $this->assertNotEmpty(\PHPPE\View::css(), "CSS"); \PHPPE\View::jslib("test.js", "testjs();"); \PHPPE\View::jslib("test2.js", "testjs();"); \PHPPE\View::jslib(url("js", "test.js"), "testjs();"); $this->assertNotEmpty(\PHPPE\View::jslib(), "JSLib"); \PHPPE\View::js("some()", "thing();"); \PHPPE\View::js("some2()", "thing();", true); \PHPPE\View::menu("a", "b"); \PHPPE\View::menu("c", ["d" => "e"]); $this->assertNotEmpty(\PHPPE\View::menu(), "Menu"); $o = \PHPPE\Core::$core->output; \PHPPE\Core::$core->output = "ncurses"; $o = trim(\PHPPE\View::e('D', "message", "module")); $this->assertEquals("module-D: message" . chr(27) . "[0m", $o, "error string #1"); \PHPPE\Core::$core->output = "html"; $this->assertEquals("<span style='background:#F00000;color:#FEA0A0;padding:3px;'>E-module: ["message"]</span>", trim(\PHPPE\View::e('E', ["message"], "module")), "error string #2"); \PHPPE\Core::$core->output = $o; $this->assertEquals("aaa\n<!include test2>\nbbb\n", \PHPPE\View::get("test1"), "Raw template file"); \PHPPE\DS::close(); $ds = new \PHPPE\DS("sqlite::memory:"); \PHPPE\DS::exec("UPDATE views SET css='[\"a.css\"]' WHERE id='simple'"); $this->assertNotEmpty(\PHPPE\View::get("simple"), "Raw template db"); \PHPPE\DS::exec("UPDATE views SET css='' WHERE id='simple'"); }
function init() { View::jslib("resptable.js", "pe.resptable.init();"); }
function edit() { View::jslib("setsel.js", "pe.setsel.search('" . addslashes($this->fld) . "');"); View::css("setsel.css"); $out = [0 => "", 1 => ""]; $a = $this->attrs; $opts = !empty($a[0]) && $a[0] != "-" ? View::getval($a[0]) : []; if (is_string($opts)) { $opts = str_getcsv($opts, ","); } if (!is_array($opts)) { $opts = ""; } if (is_string($this->value)) { $val = explode(",", $this->value); } elseif (isset($this->value[0]['id'])) { $val = []; foreach ($this->value as $k => $v) { $val[$k] = $v['id']; } } else { $val = $this->value; } if (empty($val)) { $val = []; } $i = @array_flip($val); $b = []; if (!empty($this->args[2])) { if (!is_array($this->args[2])) { $f = @array_flip(explode(",", $this->args[2])); } else { $f = $this->args[2]; } } else { $f = []; } $flt = []; $filters = []; $idx = []; foreach ($f as $k => $v) { $d = explode(":", $k); if (!empty($d[1])) { $filters[$d[0]] = $v; $idx[$d[0]] = $d[1]; } else { $filters[$d[0]] = $v; } } foreach ($opts as $k => $v) { $id = $k; $name = $v; $title = ""; $blk = "<div"; $rep = !empty($this->args[3]) ? $this->args[3] : "%name%"; $rep = str_ireplace("%KEY%", $k, $rep); if (is_array($v) || is_object($v)) { foreach ($v as $K => $V) { if (isset($filters[$K])) { $flt[$K][$V] = !empty($idx[$K]) && !empty($v[$idx[$K]]) ? $v[$idx[$K]] : $V; } if (!is_scalar($V)) { continue; } $rep = str_ireplace("%" . $K . "%", $V . "", $rep); if (!empty($this->args[5]) && $K == trim($this->args[5])) { $title = $V; } if ($K == "name") { $name = $V; } else { if ($K == "id") { $id = $V; } else { $blk .= " data-" . $K . "=\"" . htmlspecialchars($V) . "\""; } } } } else { $rep = str_ireplace("%VALUE%", $v, $rep); } $blk .= " class='setsel_item" . (!empty($this->args[0]) && isset($i[$id]) ? " setsel_itemactive" : "") . (!empty($a[2]) && $a[2] != "-" ? " " . $a[2] : "") . "'"; if ($title) { $blk .= " title='" . htmlspecialchars($title) . "'"; } $blk .= " data-id='" . htmlspecialchars($id) . "' draggable='false' onmousedown='return pe.setsel." . (empty($this->args[0]) ? "drag" : "select") . "(event,\"" . $this->fld . "\");':display>" . $rep . "</div>"; if (isset($i[$id])) { $b[$i[$id]] = str_replace(":display", "", $blk); } $blk = str_replace(":display", empty($this->args[0]) && isset($i[$id]) ? " data-inlist='1' style='display:none;'" : "", $blk); if (empty($this->args[0]) || !isset($i[$id])) { $out[1] .= $blk; } else { $out[1] = $blk . $out[1]; } } if (empty($this->args[0])) { ksort($b); } else { $out[1] .= "<div class='setsel_item" . (!empty($this->args[0]) && empty($b) ? " setsel_itemactive" : "") . (!empty($a[2]) && $a[2] != "-" ? " " . $a[2] : "") . "' data-id='' onmousedown='return pe.setsel.select(event,\"" . $this->fld . "\");' style='text-align:center;font-style:italic;'> * " . L("None") . " *</div>"; } if (isset($flt['lang'])) { unset($flt['lang']); foreach ($_SESSION['pe_ls'] as $l => $v) { $flt['lang'][$l] = $l . " " . L($l); } } $flthtml = !empty($a[3]) ? $a[3] : ""; foreach ($filters as $f => $v) { if ($f) { $flthtml .= "<select class='setsel_input' name='" . $f . "' onchange='pe.setsel.search(\"" . $this->fld . "\");' style='margin-right:5px;'><option value=''>*</option>"; if (is_array($flt[$f])) { foreach ($flt[$f] as $F => $V) { if (!empty($F) && !empty($V)) { $flthtml .= "<option value=\"" . htmlspecialchars($F) . "\"" . (@$_REQUEST['setsel_' . $f] == $F ? " selected" : "") . ">" . L($V) . "</option>"; } } } $flthtml .= "</select>"; } } $out[0] = implode("", $b); return "<div class='setsel'><input type='hidden' id='" . $this->fld . "' name='" . $this->fld . "' value='" . htmlspecialchars(@implode(",", $val)) . "'>" . "<div id='" . $this->fld . ":filters' class='setsel_filters'>" . (!empty($this->args[4]) ? "<span class='setsel_title' style='float:left;line-height:22px !important;'>" . $this->args[4] . "</span>" : "") . $flthtml . "<input name='search' class='setsel_input' type='text' placeholder='" . L("Search") . "' onchange='pe.setsel.search(\"" . $this->fld . "\");' onkeyup='pe.setsel.search(\"" . $this->fld . "\");'>" . "<span style='font-size:20px;padding-left:5px;padding-right:5px;'>⌕</span><br style='clear:both;'/></div>\n" . "<div class='" . $this->css . " " . (!empty($a[1]) && $a[1] != "-" ? $a[1] : "") . " setsel_box' onmouseup=\"pe.dnd.drop(event,pe.setsel.remove);\" id='" . $this->fld . ":all' style='height:" . intval(!empty($this->args[1]) ? $this->args[1] : 128) . "px;" . (!empty($this->args[0]) ? "width:100% !important;" : "") . "box-sizing:border-box;overflow:auto;'>" . $out[1] . "</div>" . (empty($this->args[0]) ? "<div class='" . $this->css . " " . (!empty($a[1]) && $a[1] != "-" ? $a[1] : "") . " setsel_box' onmouseover='pe.setsel.droparea(event);' onmouseup=\"pe.dnd.drop(event,pe.setsel.add);\" id='" . $this->fld . ":inlist' style='height:" . intval(!empty($this->args[1]) ? $this->args[1] : 128) . "px;padding-bottom:64px;box-sizing:border-box;overflow:auto;'>" . $out[0] . "</div>" : "") . "</div>"; }
/** * default action, loaded via AJAX */ function action($item) { //! assets if (!empty($_REQUEST['assetn'])) { list($d, $f) = explode("/", $_REQUEST['assetn']); $fn = ".tmp/" . session_id() . "/" . $d . "/" . $f; if (file_exists($fn)) { header("Content-type: " . ($d == "i" ? "image/png" : ($d == "c" ? "text/css" : ($d == "j" ? "text/javascript" : "application/octet-stream")))); die(file_get_contents($fn) . ""); } } //! sitebuild import Core::$core->noframe = true; $import = Core::req2arr("import"); //! uncompress uploaded archive if (!empty($import['file']['tmp_name'])) { @Tools::rmdir(".tmp/" . session_id()); @mkdir(".tmp/" . session_id() . "/i", 0750, true); @mkdir(".tmp/" . session_id() . "/c", 0750, true); @mkdir(".tmp/" . session_id() . "/j", 0750, true); @mkdir(".tmp/" . session_id() . "/f", 0750, true); @mkdir(".tmp/" . session_id() . "/h", 0750, true); Tools::untar($import['file']['tmp_name'], function ($name, $body) { $fn = ""; if (substr($name, -4) == ".htm" || substr($name, -5) == ".html") { self::$success = true; $fn = "h/" . basename($name); } else { if (in_array(substr($name, -4), [".gif", ".png", ".pnm", ".jpg", ".svg"])) { $fn = "i/" . basename($name); } else { if (in_array(substr($name, -4), [".eot", ".ttf"]) || substr($name, -5) == ".woff" || substr($name, -6) == ".woff2") { $fn = "f/" . basename($name); } else { if (substr($name, -4) == ".css") { $fn = "c/" . basename($name); } else { if (substr($name, -3) == ".js") { $fn = "j/" . basename($name); } } } } } if (!empty($fn)) { file_put_contents(".tmp/" . session_id() . "/" . $fn, $body); } }); if (!self::$success) { Core::error("Bad archive"); return; } } //! choose a html $this->htmls = glob(".tmp/" . session_id() . "/h/*"); if (count($this->htmls) == 1) { $item = 1; } if (intval($item) > 0 && !empty($this->htmls[$item - 1])) { $html = $this->htmls[$item - 1]; unset($this->htmls); } if (empty($html)) { if (intval($item) > 0) { Core::error("Bad archive"); } return; } //! choose application area $data = preg_replace("/<script.*?\\/script>/ims", "", file_get_contents($html)); $files = glob(".tmp/" . session_id() . "/*/*"); $assets = ["i" => "images", "c" => "css", "j" => "js", "f" => "fonts"]; //! replace urls with temporary ones for the uploaded files foreach ($files as $f) { if (!empty($assets[basename(dirname($f))])) { $data = preg_replace("/[^=\\ \t\r\n\\'\",\\(\\[]+" . basename($f) . "/ims", url("cms/sitebuild") . "?assetn=" . basename(dirname($f)) . "/" . basename($f), $data); } if (basename(dirname($f)) == "c") { View::css(url("cms/sitebuild") . "?assetn=" . basename(dirname($f)) . "/" . basename($f)); } else { if (basename(dirname($f)) == "j") { View::jslib(url("cms/sitebuild") . "?assetn=" . basename(dirname($f)) . "/" . basename($f)); } } } //! get the main application tag $this->content = CMS::taghtml($data); if (empty($_REQUEST['chooseid']) && preg_match("/(<[^<>]*?id=[\\'\"]?content[^>]*?>)/ims", $this->content, $m) && !empty($m[0]) && preg_match("/data\\-chooseid=[\\'\"]?([0-9]+)/ims", $m[0], $M)) { $_REQUEST['chooseid'] = $M[1]; } if (!empty($_REQUEST['chooseid'])) { //! replace with <!app> $t = CMS::splithtml($this->content, $_REQUEST['chooseid'], 0) . "<!app>" . CMS::splithtml($this->content, $_REQUEST['chooseid'], 2); //! replace temporary urls with final ones preg_match_all("/[^=\\ \t\r\n\\'\",\\(\\[]+\\?assetn=([a-z])\\/([^=\\ \t\r\n\\'\",\\)\\]]+)/ims", $t, $m, PREG_SET_ORDER); foreach ($m as $M) { $t = str_replace($M[0], $assets[$M[1]] . "/" . $M[2], $t); } //! figure out sitebuild name $name = strtr(basename($html), [".html" => "", ".htm" => ""]); if ($name == "index" || $name == "frame" || $name == "simple" || $name == "default") { $name = "sitebuild" . Core::$core->now; } $views = Views::find($name); if (!empty($views)) { $name .= Core::$core->now; } //! save sitebuild $view = new Views(); $view->id = $name; $view->name = $name; $view->sitebuild = $name; $view->data = preg_replace("/<!\\-\\-.*?\\-\\->/ms", "", $t); $view->created = date("Y-m-d H:i:s", Core::$core->now); foreach ($files as $f) { if (empty($assets[basename(dirname($f))])) { continue; } if (basename(dirname($f)) == "c") { $view->css[] = basename($f); } if (basename(dirname($f)) == "j") { $view->jslib[] = basename($f); } } if ($view->save(true)) { //! copy temporary files to public directory foreach ($assets as $k => $v) { chdir(".tmp/" . session_id() . "/" . $k); Tools::copy(glob("*"), "public/" . $v); chdir("../../.."); } //! clean up @Tools::rmdir(".tmp/" . session_id()); //! redirect user to the fresh new sitebuild layout Http::redirect("cms/layouts/" . $name); } Core::error("Unable to save sitebuild!"); } }