/** * Action handler */ function client($item = null) { $node = Core::lib("ClusterCli"); // get command $cmd = DS::field("cmd", self::$_table, "id=?", "", "", [$node->id]); // keep alive signal $d = @file_get_contents("/proc/loadavg"); $l = !empty($d) ? explode(" ", $d)[0] : "1.0"; if (empty(DS::exec("UPDATE " . self::$_table . " SET modifyd=CURRENT_TIMESTAMP,cmd='',load=? WHERE id=?", [$l, $node->id]))) { DS::exec("INSERT INTO " . self::$_table . " (id,name,load,type,created,modifyd) VALUES (?,?,?,'" . ($node->_loadbalancer ? "lb" : "worker") . "',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)", [$node->id, gethostbyaddr($node->id), $l]); } // execute commands if ($cmd == "invalidate") { Cache::invalidate(); } elseif ($cmd == "shutdown") { exec("sudo poweroff"); } elseif ($cmd == "restart") { exec("sudo restart"); } elseif ($cmd == "reload" && $node->_loadbalancer) { // generate bind config $bind = Core::lib("ClusterCli")->bindcfg(); // call the shell hook and pass the config to it's stdin if (!empty($bind) && file_exists(static::$_cmd)) { $p = popen(". " . static::$_cmd . " " . $cmd, "w"); if ($p) { pwrite($p, $bind); pclose($p); } } } }
/** * 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"); } } }
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'"); }
public function testDataSource() { \PHPPE\DS::close(); $wasExc = false; try { $val = \PHPPE\DS::field("1+1"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No DS exception"); $ds = new \PHPPE\DS("sqlite::memory:"); $this->assertEquals("%a%", \PHPPE\DS::like("a"), "like #1"); $this->assertEquals("%a%b%", \PHPPE\DS::like("a b"), "like #2"); $this->assertEquals("%a%b%", \PHPPE\DS::like("a' \"b?"), "like #3"); \PHPPE\DS::db()->s = ["describe" => ".schema"]; $this->assertInstanceOf("PDO", \PHPPE\DS::db(), "PDO object"); $this->assertEquals(2, \PHPPE\DS::field("1+1"), "field"); $wasExc = false; try { $val = \PHPPE\DS::query("*", "nosuchtable"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No scheme exception"); $wasExc = false; try { $val = \PHPPE\DS::query("*", "badtable"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Bad scheme exception"); $this->assertEquals('a:4:{i:0;a:3:{s:2:"id";s:1:"1";s:4:"name";s:5:"first";s:8:"parentId";s:1:"0";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}i:2;a:3:{s:2:"id";s:1:"3";s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}i:3;a:3:{s:2:"id";s:1:"4";s:4:"name";s:6:"fourth";s:8:"parentId";s:1:"0";}}', serialize(\PHPPE\DS::query("*", "test")), "scheme install"); $this->assertEquals(1, \PHPPE\DS::exec("-- sql comment"), "exec comment"); $this->assertEquals(1, \PHPPE\DS::exec("insert into test values (5,'fifth',0)"), "exec insert"); $this->assertEquals(1, \PHPPE\DS::exec("update test set parentId=1 where id=?", 5), "exec update unnamed"); $this->assertEquals(1, \PHPPE\DS::query("parentId", "test", "id=5")[0]["parentId"], "exec parentIdcheck"); $this->assertEquals(1, \PHPPE\DS::exec("update test set parentId=:newparent where id=:id", [":id" => 5, ":newparent" => 2]), "exec update named"); $this->assertEquals(2, \PHPPE\DS::query("parentId", "test", "id=5")[0]["parentId"], "exec parentIdcheck"); $this->assertEquals(0, \PHPPE\DS::exec("update test set parentId=:newparent where id=:id", [":id" => 6, ":newparent" => 2]), "exec update no match"); $this->assertInternalType("integer", \PHPPE\DS::exec("update test set parentId=1 where id=?", 5), "insert returns"); $this->assertInternalType("array", \PHPPE\DS::exec("select * from test"), "select returns"); $this->assertEquals('a:2:{i:0;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}i:1;a:3:{s:2:"id";s:1:"5";s:4:"name";s:5:"fifth";s:8:"parentId";s:1:"1";}}', serialize(\PHPPE\DS::query("*", "test", "id!=4", "parentId")), "query group by"); $this->assertEquals('a:2:{i:0;a:3:{s:2:"id";s:1:"5";s:4:"name";s:5:"fifth";s:8:"parentId";s:1:"1";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}}', serialize(\PHPPE\DS::query("*", "test", "id!=4", "parentId", "id desc")), "query order by"); $this->assertEquals('a:2:{i:0;a:3:{s:2:"id";s:1:"5";s:4:"name";s:5:"fifth";s:8:"parentId";s:1:"1";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}}', serialize(\PHPPE\DS::query("*", "test", "id!=4", "parentId", "id desc", 1)), "query offset"); $this->assertEquals('a:1:{i:0;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}}', serialize(\PHPPE\DS::query("*", "test", "id!=4", "parentId", "id desc", 1, 1)), "query limit"); $this->assertEquals('a:0:{}', serialize(\PHPPE\DS::query("*", "test", "id=11")), "query empty"); $this->assertEquals('a:2:{s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}', serialize(\PHPPE\DS::fetch("name,parentId", "test", "id=?", "", "", [3])), "fetch record"); $this->assertEquals('a:0:{}', serialize(\PHPPE\DS::fetch("*", "test", "id=11")), "fetch empty"); $this->assertEquals('a:3:{i:0;a:4:{s:2:"id";s:1:"1";s:4:"name";s:5:"first";s:8:"parentId";s:1:"0";s:1:"_";a:2:{i:0;a:3:{s:2:"id";s:1:"3";s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}i:1;a:3:{s:2:"id";s:1:"5";s:4:"name";s:5:"fifth";s:8:"parentId";s:1:"1";}}}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}i:2;a:3:{s:2:"id";s:1:"4";s:4:"name";s:6:"fourth";s:8:"parentId";s:1:"0";}}', serialize(\PHPPE\DS::tree("select * from test where parentId=:id")), "tree all"); $this->assertEquals('a:2:{i:0;a:3:{s:2:"id";s:1:"3";s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}i:1;a:3:{s:2:"id";s:1:"5";s:4:"name";s:5:"fifth";s:8:"parentId";s:1:"1";}}', serialize(\PHPPE\DS::tree("select * from test where parentId=:id", 1)), "tree sub-tree"); $this->assertEquals('a:0:{}', serialize(\PHPPE\DS::tree("select * from test where parentId=:id", 3)), "tree empty"); $wasExc = false; try { $val = \PHPPE\DS::query("nocolumn", "test"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "PDO exception"); $wasExc = false; try { $val = \PHPPE\DS::db("nodrv:"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No driver exception"); $this->assertGreaterThan(0, \PHPPE\DS::bill(), "Billed secs"); }
function flush($item = null) { header("Content-type:text/plain"); if (Core::$client->ip != "CLI" && !Core::$user->has("cluadm")) { Http::redirect("403"); } $node = Core::lib("ClusterSrv"); DS::exec("UPDATE " . self::$_table . " SET cmd='invalidate' WHERE type='worker'"); DS::exec("UPDATE " . self::$_table . " SET cmd='reload' WHERE type='lb'"); if ($node->_master) { $node->resources("reload"); } die("OK\r\n"); }
public function testContent() { Core::$core->nocache = true; include_once __DIR__ . "/../libs/FailFilter.php"; DS::close(); DS::db("sqlite::memory:"); DS::exec("insert into pages (id,name,template,data,dds,filter) values ('test','Test','testview','{\"body\":\"testbody\"}','{\"testdds\":[\"1\",\"\",\"\"]}','fail');"); DS::exec("insert into views (id,ctrl) values ('testview', 'echo(\"OK\");');"); $url = Core::$core->url; $title = Core::$core->title; $contentApp = new Content(); //! no content Core::$core->title = "NONE"; $contentApp = new Content("no/such/content"); $this->assertEquals("NONE", Core::$core->title, "No content"); //! filter $contentApp = new Content("test/"); $this->assertEquals("403", Core::$core->template, "Filtered"); DS::exec("update pages set filter='' where id='test';"); //! is content $contentApp = new Content("test/"); $this->assertEquals("Test", Core::$core->title, "Content"); $contentApp->getDDS($contentApp); $this->assertEquals("testbody", $contentApp->body, "Body"); $this->assertNotEmpty($contentApp->testdds, "DDS"); $old = Core::$core->noctrl; Core::$core->noctrl = false; $contentApp->ctrl = "echo('OK');"; $this->assertEquals("OK", $contentApp->action(), "Content controller #1"); $old = Core::$core->noctrl; Core::$core->noctrl = true; $this->assertNull($contentApp->action(), "Content controller #2"); Core::$core->noctrl = $old; DS::exec("update pages set dds='{\"testdds2\":[\"nosuchcolumn\",\"\",\"\"]}' where id='test';"); $contentApp = new Content("test/"); $contentApp->getDDS($contentApp); $this->assertEmpty(@$contentApp->testdds2, "DDS failed"); Core::$core->title = $title; }
/** * 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; }