function action($item = null) { if (Core::$client->ip != "CLI") { Http::redirect("403"); } $node = Core::lib("ClusterCli"); echo chr(27) . "[96mThis node is: " . chr(27) . "[0m" . $node->id . " " . (!empty($node->loadbalancer) ? "LOADBALANCER" : "WORKER") . "\n"; }
/** * 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 __construct() { //! check if executed from CLI if (Core::$client->ip != "CLI") { Http::redirect("403"); } //! convert source to deployment format Repository::compress(); //! update document Repository::updateDoc(); }
function __construct() { //! check if executed from CLI if (Core::$client->ip != "CLI") { Http::redirect("403"); } if (empty($_SERVER['argv'][2])) { die(Lang::getUsage()); } //! parse files for translateable strings Lang::parse($_SERVER['argv'][2], @$_SERVER['argv'][3], @$_SERVER['argv'][4]); }
function action() { if (isset($_REQUEST['clearbenchmark'])) { Benchmark::clear(); Http::redirect(); } $this->data = Benchmark::stats(); foreach ($this->data as $u => $v) { $this->urls[] = $u . " (" . sprintf(L("%d samples"), reset($v)['cnt']) . ")"; } View::js("choosediv(value)", "var divs=document.querySelectorAll('DIV.benchmark');for(var i in divs) divs[i].style.display=divs[i].id=='url'+value?'block':'none';"); }
function __construct() { //! check if executed from CLI if (Core::$client->ip != "CLI") { Http::redirect("403"); } if (empty($_SERVER['argv'][2])) { die(Templates::getUsage()); } //! create file from template Templates::create($_SERVER['argv'][2]); }
function action() { //! check if executed from CLI if (Core::$client->ip != "CLI") { Http::redirect("403"); } if (empty($_SERVER['argv'][2])) { die(Pretty::getUsage()); } //! format files Pretty::parse($_SERVER['argv'][2]); }
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 testGet() { $this->assertEquals('OK', trim(\PHPPE\Http::get(url("tests", "http"))), "http get"); $this->assertEquals('{"var1":"test1","var2":"test2"}', \PHPPE\Http::get(url("tests", "http") . "post", ["var1" => "test1", "var2" => "test2"]), "http post"); $this->assertEquals('Redirected', trim(\PHPPE\Http::get(url("tests", "http") . "redirect")), "http redirect"); $this->assertEquals('OK', trim(\PHPPE\Http::get(url("tests", "http") . "cookie")), "http cookie change"); \PHPPE\Core::$client->lang = "xx"; $this->assertEquals('xx;q=0.8', trim(\PHPPE\Http::get(url("tests", "http") . "language")), "http language"); $this->assertEquals('CR', trim(\PHPPE\Http::get(url("tests", "http") . "cr1")), "http cr #1"); $this->assertEquals("C\rR", trim(\PHPPE\Http::get(url("tests", "http") . "cr2")), "http cr #2"); $this->AssertEmpty(trim(\PHPPE\Http::get("badurl")), "http bad url #1"); $this->AssertEmpty(trim(\PHPPE\Http::get("badurl://localhost")), "http bad url #2"); $this->AssertEmpty(trim(\PHPPE\Http::get(url("tests", "http"), "", 3, 8)), "http recursion"); $this->AssertEmpty(trim(\PHPPE\Http::get(url("tests", "http") . "timeout")), "http timeout"); }
public function testRoute() { // route test $routes = \PHPPE\Http::route(); $this->assertNotEmpty($routes[sha1("tests|Developer|")], "Route exists"); //add different new routes \PHPPE\Http::route("test1", "Tests"); \PHPPE\Http::route("test2", "Tests", "action_run"); \PHPPE\Http::route(array("url" => "test3", "name" => "Tests", "action" => "action_run")); \PHPPE\Http::route(array(array("test4", "Tests", "action_run"), array("test5", "Tests", "action_run"))); $r = new \stdClass(); $r->url = "test6"; $r->name = "Tests"; \PHPPE\Http::route($r); \PHPPE\Http::route("test7", "Tests", "action_member", "@loggedin,admin"); \PHPPE\Http::route("test7", "Tests", "action_public"); \PHPPE\Http::route("test9", "Tests", "", array("@loggedin", "admin")); $wasExc = false; try { \PHPPE\Http::route(new \stdClass()); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Bad route"); $new = \PHPPE\Http::route(); $this->assertGreaterThan(count($routes), count($new), "New routes"); $this->assertNotEmpty($new[sha1("test1|Tests|")], "Route added"); $this->assertEmpty($new[sha1("test1|Tests|")][2], "Default action"); $this->assertNotEmpty($new[sha1("test2|Tests|action_run")][2], "Named action"); $this->assertNotEmpty($new[sha1("test3|Tests|action_run")][1], "Assoc array route"); $this->assertNotEmpty($new[sha1("test4|Tests|action_run")][2], "Multiple route #1"); $this->assertNotEmpty($new[sha1("test5|Tests|action_run")][2], "Multiple route #2"); $this->assertNotEmpty($new[sha1("test6|Tests|")], "Object route"); $this->assertEquals($new[sha1("test7|Tests|action_member")][0], $new[sha1("test7|Tests|action_public")][0], "Same route with and without filter"); $this->assertEquals(serialize($new[sha1("test7|Tests|action_member")][3]), serialize($new[sha1("test9|Tests|")][3]), "Filter as string and as array"); //! for PHPUnit, as it runs from /usr/local/bin // \PHPPE\Core::$core->base="localhost/"; $data1 = file_get_contents(url("tests", "httptest") . "?nojs"); $data2 = file_get_contents(url("tests", "httptest") . "?nojs", false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['var1' => '1', 'var2' => '2'])]])); $this->assertEquals("GET", $data1, "Same url with GET filter"); $this->assertEquals("POST", $data2, "Same url with POST filter"); $this->assertEquals('["test1","run",[]]', json_encode(\PHPPE\Http::urlMatch("test1", "run")), "urlMatch #1"); $this->assertEquals('["Tests","action_run",[]]', json_encode(\PHPPE\Http::urlMatch("", "", "test4/")), "urlMatch #2"); \PHPPE\Core::$user->id = 0; $this->assertEquals('["403","run",[]]', json_encode(\PHPPE\Http::urlMatch("", "run", "test9/")), "urlMatch #3"); }
/** * default action */ function action($item) { //! page history enabled? $this->revert = Core::lib("CMS")->revert; //! delete a page with all versions if (!empty($_REQUEST['pagedel'])) { Page::delete($_REQUEST['pagedel']); unset($_SESSION['cms_url']); Http::redirect(); } //! publicate a page if (isset($_REQUEST['publish']) && $this->revert) { $this->ispublish = true; $publish = array_keys(Core::req2arr('publish')); if (!empty($publish)) { Page::publish($publish); Http::redirect("cms/pages"); } } //! load languages $this->langs[''] = "*"; foreach (!empty($_SESSION['pe_ls']) ? $_SESSION['pe_ls'] : ['en' => 1] as $l => $v) { $this->langs[$l] = $l . " " . L($l); } //! unlock old pages for this user if any Page::unLock(Core::$user->id); //! get list of pages $pages = Page::getPages(intval(@$_REQUEST['order'])); $needcleanup = false; foreach ($pages as $p) { if ($this->ispublish && ($p['publishid'] != 0 || $p['ownerid'] != 0)) { continue; } if ($this->revert && $p['publishid'] == 0) { $this->needpublish = true; } if (!$this->revert && $p['versions'] > 1) { $needcleanup = true; } $this->pages[empty($_REQUEST['order']) ? 0 : (empty($p['template']) ? $p['tid'] : $p['template'])][] = $p; } //! this is required once after page history turned off if ($needcleanup) { Page::cleanUp($pages); } }
function __construct() { //! check if executed from CLI if (Core::$client->ip != "CLI") { Http::redirect("403"); } //! run tests if (in_array("--tests", $_SERVER['argv'])) { echo "Running tests: "; ob_start(); $tests = Testing::doTests(); $d = ob_get_clean(); if (!$tests) { die("FAILED\n{$d}"); } echo "OK\n"; unset($d); } //! *** MKREPO Event *** Core::event("mkrepo"); //! create repository Repository::make(); die; }
public static function filter() { if (\PHPPE\Core::$user->id) { return true; } \PHPPE\Http::redirect('login', 1); }
function http($item) { switch ($item) { //return POST array case "post": die(json_encode($_POST)); //force time out //force time out case "timeout": sleep(1.001); die(""); //redirect url //redirect url case "redirect": \PHPPE\Http::redirect(url() . "redirect2"); case "redirect2": die("Redirected"); //cookie handler test //cookie handler test case "cookie": setcookie("test", "1"); \PHPPE\Http::redirect(url() . "cookie2"); case "cookie2": if (!isset($_COOKIE['test'])) { die; } setcookie("test", "2"); \PHPPE\Http::redirect(url() . "cookie3"); case "cookie3": if ($_COOKIE['test'] != "2") { die; } die("OK"); //language code pass over //language code pass over case "language": die($_SERVER['HTTP_ACCEPT_LANGUAGE']); //automatic carridge return removal for text //automatic carridge return removal for text case "cr1": die("C\rR"); case "cr2": header("Content-type: image/png"); die("C\rR"); //for output cache test //for output cache test case "cachetest": \PHPPE\Core::$core->template = "cachetest"; if (!empty($_REQUEST['skipcache'])) { $this->iscached = "NOCACHE"; } break; //default test case //default test case default: die("OK " . $item); } }
/** * 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!"); } }
function deploy($item = null) { header("Content-type:text/plain"); if (Core::$client->ip != "CLI" && !Core::$user->has("cluadm")) { Http::redirect("403"); } if (!file_exists(".tmp/multiserver")) { die("CLUSTER-E: " . L("multiserver not installed") . "\n"); } $node = Core::lib("ClusterSrv"); if (!$node->_master) { die("CLUSTER-E: " . L("not master") . "\n"); } if (empty($node->_deploy) || empty($node->_skeleton)) { die("CLUSTER-E: " . L("deploy not configured") . "\n"); } // deploy['role'] => [ directories to sync ] $nodes = DS::query("*", self::$_table, "", "", "type,load DESC,id"); $allOk = 1; foreach ($nodes as $n) { $t = $n['type']; if ($t == "master" || $t == "slave") { $t = "admin"; } echo $n['id'] . " "; if (empty($node->_deploy[$t])) { echo L("no such role") . " " . $t; $allOk = 0; } else { try { parent::deploypush($node->_skeleton, $node->_deploy[$t], $n); echo chr(27) . "[92mOK" . chr(27) . "[0m"; } catch (\Exception $e) { echo chr(27) . "[91m" . $e->getMessage() . chr(27) . "[0m"; $allOk = 0; } } echo "\r\n"; } die(($allOk ? chr(27) . "[92mOK" : chr(27) . "[91mERR") . chr(27) . "[0m\r\n"); }
/** * default action */ function action($item) { //! check access rights if (!Core::$user->has("siteadm|webadm")) { Http::redirect("403"); } //! if we have to delete a page version if (isset($_REQUEST['pagedel'])) { if (!empty($_REQUEST['created'])) { Page::delete($item, $_REQUEST['created']); } Http::redirect($item); } //! revert to an old version of the page if (isset($_REQUEST['revert'])) { if (!empty($_REQUEST['created'])) { Core::log('A', sprintf("Page %s reverted to %s by %s", $item, $_REQUEST['created'], Core::$user->name), "cmsaudit"); DS::exec("UPDATE pages set created=CURRENT_TIMESTAMP,modifyd=CURRENT_TIMESTAMP,modifyid=? WHERE id=? AND (lang='' OR lang=?) AND created=?", [Core::$user->id, $item, Core::$client->lang, $_REQUEST['created']]); } Http::redirect($item); } //! get the latest public version's date if (empty($_REQUEST['created'])) { $_REQUEST['created'] = DS::field("created", "pages", "(id=? OR ? LIKE id||'/%') AND (lang='' OR lang=?) AND publishid!=0", "", "id DESC,created DESC", [$item, $item, Core::$client->lang]); if (empty($_REQUEST['created'])) { $_REQUEST['created'] = DS::field("created", "pages", "(id=? OR ? LIKE id||'/%') AND (lang='' OR lang=?)", "", "id DESC,created ASC", [$item, $item, Core::$client->lang]); } } //! load frame page for page parameters and dds $frame = DS::fetch("data,dds", "pages", "id='frame' AND (lang='' OR lang=?) AND created<=?", "", "", [Core::$client->lang, $_REQUEST['created']]); $frame['dss'] = @json_decode($frame['dds'], true); $frame['data'] = @json_decode($frame['data'], true); View::assign("frame", $frame['data']); //! load archive version //! normally you would use Model, but that would only return the latest version $page = DS::fetch("*", "pages", "(id=? OR ? LIKE id||'/%') AND (lang='' OR lang=?) AND created=?", "", "id DESC,created DESC", [$item, $item, Core::$client->lang, $_REQUEST['created']]); $this->title = $page['name']; $title = L("ARCHIVE") . " " . $page['name']; if (is_string($page['data'])) { $page['data'] = @json_decode($page['data'], true); } if (is_array($page['data'])) { foreach ($page['data'] as $k => $v) { $this->{$k} = $v; } } foreach (["id", "name", "lang", "filter", "template", "pubd", "expd", "dds", "ownerid", "created"] as $k) { $this->{$k} = $page[$k]; } $p = @array_merge($frame['dds'], @json_decode($page['dds'], true)); if (is_array($p)) { foreach ($p as $k => $c) { if ($k != "dds") { try { $this->{$k} = DS::query($c[0], $c[1], @$c[2], @$c[3], @$c[4], @$c[5], View::getval(@$c[6])); } catch (\Exception $e) { Core::log("E", $item . " " . $e->getMessage() . " " . implode(" ", $c), "dds"); } } } } $old = View::template($this->template); //! if we have to compare to an older (or the latest) version if (isset($_REQUEST['diff'])) { include_once "vendor/phppe/CMS/libs/simplediff.php"; //! load current version $frame = DS::fetch("data,dds", "pages", "id='frame' AND (lang='' OR lang=?) AND publishid!=0", "", "created DESC", [Core::$client->lang]); $frame['dss'] = @json_decode($frame['dds'], true); $frame['data'] = @json_decode($frame['data'], true); View::assign("frame", $frame['data']); $page = DS::fetch("*", "pages", "id=? OR ? LIKE id||'/%'", "", "id DESC,created DESC", [$item, $item]); if (is_string($page['data'])) { $page['data'] = @json_decode($page['data'], true); } if (is_array($page['data'])) { foreach ($page['data'] as $k => $v) { $this->{$k} = $v; } } foreach (["id", "name", "lang", "filter", "template", "pubd", "expd", "dds", "ownerid", "created"] as $k) { $this->{$k} = $page[$k]; } $p = @array_merge($frame['dds'], @json_decode($page['dds'], true)); if (is_array($p)) { foreach ($p as $k => $c) { if ($k != "dds") { try { $this->{$k} = DS::query($c[0], $c[1], @$c[2], @$c[3], @$c[4], @$c[5], View::getval(@$c[6])); } catch (\Exception $e) { Core::log("E", $item . " " . $e->getMessage() . " " . implode(" ", $c), "dds"); } } } } $this->title = $page['name']; $curr = View::template($this->template); //! make sure diff splits on tag end $this->result = htmlDiff(preg_replace("/>([^\\ \t\n])/m", "> \\1", $old), preg_replace("/>([^\\ \t\n])/m", "> \\1", $curr)); //! remove diff inside tags $this->result = preg_replace("/(<[^<>]+)<ins>.*?<\\/ins>([^<>]*>)/ims", "\\1\\2", $this->result); $this->result = preg_replace("/(<[^<>]+)<del>(.*?)<\\/del>([^<>]*>)/ims", "\\1\\2\\3", $this->result); } else { $this->result = $old; } $this->title = $title; }