public function testUsers() { if (!\PHPPE\ClassMap::has("PHPPE\\Users")) { $this->markTestSkipped(); } // destroy session user $_SESSION['pe_u'] = []; $user = new \PHPPE\Users(); $this->assertNull($user->login("admin", "changeme"), "Bad username or password"); $this->assertNotNull($user->login("bzt", "changeme"), "Login"); $this->assertNull($user->login("bzt", "changeme"), "Already logged in"); $user->logout(); }
public function testCore() { $this->assertTrue(ClassMap::has("NotLoaded", "oneMethod"), "ClassMap has"); @unlink(ClassMap::$file); @unlink(ClassMap::$ace); $_SERVER['REQUEST_URI'] = ""; $_SERVER['argv'][1] = "test"; @($core = new Core(true)); $_SERVER['REQUEST_URI'] = "/test/something/?arg=1"; @($core = new Core(true)); $this->assertFileExists(ClassMap::$file, "New classmap"); $this->assertNotEmpty($core->base, "Base"); $this->assertNotEmpty($core->url, "Url"); $this->assertEquals(Core::$core->output, $core->output, "Output"); $this->assertGreaterThan(Core::$core->now, Core::started(), "Started"); $this->assertNotEmpty(ClassMap::ace(), "ClassMap access control entries"); }
public function testDB() { if (!\PHPPE\ClassMap::has("PHPPE\\DB")) { $this->markTestSkipped(); } $this->assertEquals("%some%thing%", \PHPPE\DB::like("some thing"), "like"); $this->assertEquals("SELECT * FROM users", \PHPPE\DB::select("users"), "Simple select"); $this->assertEquals("SELECT id,name FROM users", \PHPPE\DB::select("users")->fields(["id", "name"]), "Select with fields"); $this->assertEquals("SELECT * FROM users WHERE id=?", \PHPPE\DB::select("users")->where("id=?"), "Select with where #1"); $this->assertEquals("SELECT * FROM users WHERE (id=? AND name=?)", \PHPPE\DB::select("users")->where(["id=?", "name=?"]), "Select with where #2"); $this->assertEquals("SELECT * FROM users WHERE (id = 'my id')", \PHPPE\DB::select("users")->where([["id", "=", "my id"]]), "Select with where #3"); $this->assertEquals("SELECT * FROM users WHERE (id LIKE '%my%id%')", \PHPPE\DB::select("users")->where([["id", "like", "my id"]]), "Select with where #4"); $this->assertEquals("SELECT * FROM users WHERE (id LIKE '%my%id%' OR name LIKE '%my%name%') AND 1=1", \PHPPE\DB::select("users")->where([["id", "like", "my id"], ["name", "like", "my name"]], "or")->where("1=1"), "Select with where #5"); $this->assertEquals("SELECT * FROM users u, user_posts p WHERE u.id=p.id", \PHPPE\DB::select("users", "u")->table("user_posts", "p")->where("u.id=p.id"), "Select with where #6"); $wasExc = false; try { \PHPPE\DB::update("users")->where("id=id", "NOT"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "bad where exception"); $this->assertEquals("SELECT * FROM users HAVING id=?", \PHPPE\DB::select("users")->having("id=?"), "Select with having"); $this->assertEquals("SELECT * FROM users LIMIT 10", \PHPPE\DB::select("users")->limit(10), "Select with limit #1"); $wasExc = false; try { \PHPPE\DB::select("users")->offset(5)->sql(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Select with limit #2"); $this->assertEquals("SELECT * FROM users LIMIT 10 OFFSET 5", \PHPPE\DB::select("users")->limit(10)->offset(5), "Select with limit #3"); $this->assertEquals("SELECT * FROM users GROUP BY name", \PHPPE\DB::select("users")->groupBy("name"), "Select with group by #1"); $this->assertEquals("SELECT * FROM users GROUP BY name,id", \PHPPE\DB::select("users")->groupBy(["name", "id"]), "Select with group by #2"); $this->assertEquals("SELECT * FROM users ORDER BY id", \PHPPE\DB::select("users")->orderBy("id"), "Select with order by #1"); $this->assertEquals("SELECT * FROM users ORDER BY id", \PHPPE\DB::select("users")->orderBy(["id"]), "Select with order by #2"); $wasExc = false; try { \PHPPE\DB::update("users")->sql(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "update no fields exception"); $this->assertNotFalse(strpos(\PHPPE\DB::update("users"), "No fields specified"), "update no field string"); $this->assertEquals("UPDATE users SET id=?,name=?", \PHPPE\DB::update("users")->fields(['id', 'name']), "update with fields"); $this->assertEquals("DELETE FROM users", \PHPPE\DB::delete("users"), "delete table"); $this->assertEquals("DELETE a FROM users a", \PHPPE\DB::delete("users", "a")->sql(), "delete alias"); $this->assertEquals("DELETE user_posts FROM user_posts LEFT JOIN users ON user_posts.userId=users.id WHERE (users.id IS NULL)", \PHPPE\DB::delete("user_posts")->join("LEFT", "users", "user_posts.userId=users.id")->where([["users.id", "IS NULL"]]), "delete where"); $this->assertEquals("INSERT INTO users (id,name) VALUES (?,?)", \PHPPE\DB::insert("users")->fields('id,name'), "insert"); $wasExc = false; try { \PHPPE\DB::select("users")->join("SIMPLE", "user_posts", "id=id"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "bad join exception"); $this->assertEquals("REPLACE INTO users (id,name) VALUES (?,?) WHERE id=''", \PHPPE\DB::replace("users")->fields(['id', 'name'])->where("id=''"), "replace"); $this->assertEquals("TRUNCATE TABLE users", \PHPPE\DB::truncate("users"), "truncate"); $wasExc = false; try { \PHPPE\DB::select("users")->where([["1", "!="]]); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "bad where exception"); $wasExc = false; try { \PHPPE\DB::select("users")->where([["a", "similarto", "b"]]); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "bad where exception"); $wasExc = false; try { \PHPPE\DB::update("")->sql(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "no table exception"); $wasExc = false; try { \PHPPE\DB::insert("users")->sql(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "no where exception"); $wasExc = false; try { \PHPPE\DB::replace("users")->fields(["id"])->sql(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "no where exception"); \PHPPE\DS::close(); $ds = new \PHPPE\DS("sqlite::memory:"); $this->assertNotEmpty(\PHPPE\DB::select("users")->execute(), "execute"); \PHPPE\DB::truncate("users")->execute(); $wasExc = false; try { \PHPPE\DB::insert("users")->with(''); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "no fields exception"); $this->assertEquals(1, \PHPPE\DB::insert("users")->with(['id' => 123, 'name' => 'newcomer', 'email' => '*****@*****.**']), "insert with with"); $wasExc = false; try { \PHPPE\DB::select("users")->where("id=?")->execute(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "no argument exception"); $ds = new \PHPPE\DS("sqlite::memory:"); $this->assertNotEmpty(\PHPPE\DB::select("users")->execute([], 1), "execute on ds"); }
/** * default action */ function action($item) { //! omit frame and panel and disable cache Core::$core->noframe = true; Core::$core->nopanel = true; Core::$core->nocache = true; //! if not called as it should, return if (empty($item) || $item != sha1("pageadd_") && (empty($_SESSION['cms_url']) || empty($_SESSION['cms_param'][$item]))) { Core::$core->template = "403"; return; } //! get height $this->height = intval(@$_REQUEST['height']); $this->adjust = intval(@$_REQUEST['adjust']); //! save current scroll position to session so that on next //! page load pe.cms.init() will use it if (isset($_REQUEST['scrx'])) { $_SESSION['cms_scroll'] = [$_REQUEST['scrx'], $_REQUEST['scry']]; } //! get available access control entries $this->ace = ClassMap::ace(); foreach ($this->ace as $k => $v) { $this->ace[$k] = "@" . $v; } $this->ace[] = "@siteadm|webadm"; $this->ace[] = "loggedin"; $this->ace[] = "csrf"; $this->ace[] = "get"; $this->ace[] = "post"; //! get the field we're editing $F = clone $_SESSION["cms_param"][$item]; $F->fld = "page_value"; if (get_class($F) == "PHPPE\\AddOn\\wyswyg") { $F->args = [0, "pe.cms.image"]; } if (method_exists($F, 'init')) { $F->init(); } $this->fieldTitle = $F->name; $this->heightClass = @$F->heightClass; $this->boxHeight = $this->height - @$F->headerHeight; //! get the page we're editing //! if parameter name starts with "frame", load frame page instead $page = new Page(substr($F->name, 0, 6) == "frame." ? "frame" : @$_SESSION['cms_url']); $this->editable = $page->lock(); View::assign("page", $page); $n = substr($F->name, 0, 6) == "frame." ? substr($F->name, 6) : (substr($F->name, 0, 4) == "app." ? substr($F->name, 4) : $F->name); if (!empty($page->data[$n])) { $F->value = $page->data[$n]; } //! load extra data if any if (method_exists($F, 'load')) { $F->load($this); } //! save page parameter $param = Core::req2arr("page"); if (!empty($param) && $this->editable) { //! if there was no validation error if (!Core::isError()) { if (method_exists($F, "save")) { //! if it's a special field with it's own save mechanism $param['pageid'] = $page->id; if (!$F->save($param)) { Core::error(L("Unable to save page!")); } } else { //! otherwise standard page parameter $page->setParameter($F->name, $param['value']); if (!$page->save()) { Core::error(L("Unable to save page!")); } } //! close the modal if save was successful if (!Core::isError()) { //! release the page lock $page->release(); die("<html><script>parent.pe.cms.close(true);</script></html>"); } } //! copy the form data. normally you don't need to do that //! but here form name and object name differs, so it's not automatic foreach ($param as $k => $v) { $page->{$k} = $v; } } //! get the input(s) if (method_exists($F, 'edit')) { $this->field = $F->edit(); } else { //! fallback to a simple input field. Should never happen $this->field = "<input type='text' class='input" . (Core::isError("page.value") ? " errinput" : "") . "' name='page_value' value=\"" . htmlspecialchars($F->value) . "\">"; } //! focus first input View::js("init()", "var inp=document.querySelector('.reqinput,.input');if(inp!=null){inp.focus();inp.selectionStart=inp.selectionEnd=(inp.value!=null?inp.value:inp.innerHTML).length;}", true); }
/** * default action, loaded via AJAX */ function action($item) { //! available tags $list = ["/form" => "*variable [url [onsubmitjs", "/if" => "*expression", "else" => "*", "/foreach" => "*dataset", "/template" => "*", "include" => "*view", "app" => "*", "dump" => "variable", "cms" => "*addon ) variable", "=" => "expression", "L" => "label", "date" => "expression", "time" => "expression", "difftime" => "expression", "var" => "*addon ) variable", "field" => "*addon ) variable", "widget" => "*addon ) variable"]; //! Add-Ons $d = array_merge(get_declared_classes(), array_keys(ClassMap::$map)); foreach ($d as $c) { if (strtolower(substr($c, 0, 12)) == "phppe\\addon\\") { $F = new $c([], "dummy", $c, []); if (isset($F->conf) && $F->conf != "*") { $list["_" . strtolower(substr($c, 12))] = $F->conf; } unset($F); } } if (!empty($item)) { //! edit form $acl = $widget = ""; $req = $needsel = 0; if (substr($item, 0, 2) != "<!") { die(View::e("E", L("Unknown tag"))); } else { $d = ""; $c = ""; foreach ($list as $k => $v) { if ($k[0] == "_") { continue; } if (substr($item, 2, strlen($k)) == $k || "/" . substr($item, 2, strlen($k) - 1) == $k) { $d = $k[0] == "/" ? substr($k, 1) : $k; $c = $v[0] == '*' ? substr($v, 1) : $v; } } if (empty($d)) { die(View::e("E", L("Unknown tag"))); } if ($d == "=") { $d = "eval"; $a = [substr($item, 3, strlen($item) - 4)]; } else { $a = str_getcsv(preg_replace("/[\\ ]+/", " ", strtr(substr($item, 2, strlen($item) - 3), ["(," => " - ", "(" => " ", ")" => " )", ",," => " - ", "," => " "])), " "); array_shift($a); } if (substr($c, 0, 5) == "addon") { if (@$a[0][0] == "@") { $acl = substr($a[0], 1); array_shift($a); } if (@$a[0][0] == "*") { $req = 1; $a[0] = substr($a[0], 1); } $widget = array_shift($a); if (empty($widget)) { $widget = "hidden"; } $needsel = 1; } echo "<b>" . L(!empty($widget) && !empty(Core::$l[$widget]) ? $widget : "help_" . $d) . "</b><br/>\n<div id='tageditor' style='padding:5px;'><input type='hidden' name='tag' value='" . htmlspecialchars($d) . "'>\n"; if (substr($c, 0, 5) == "addon") { $t = $d == "cms" ? L("Show value") : L("Required value"); echo "<input type='checkbox' class='input' name='required' onchange='pe.cms.settag(\"tageditor\");' title=\"" . htmlspecialchars($t) . "\" value='*'" . ($req ? " checked" : "") . ">\n"; echo "<select class='input' name='widget' onchange='pe.cms.settag(\"tageditor\");pe.wyswyg.popup(event,\"layout_data\",\"cms/tag?item=" . urlencode("<!" . $d . " " . ($req ? "*" : "")) . "\"+this.value+\">\",true);' onmouseover='pe_w();'>"; foreach ($list as $k => $v) { if ($k[0] != "_") { continue; } echo "<option value='" . htmlspecialchars(substr($k, 1)) . "'" . (substr($k, 1) == $widget ? " selected" : "") . " onmouseover='pe_w();'>" . L(substr($k, 1)) . "</option>\n"; } echo "</select>\n<input type='text' class='input smallinput' name='acl' onkeydown='if(event.key==\"Enter\"){event.preventDefault();pe_p();}' onkeyup='pe.cms.settag(\"tageditor\");event.preventDefault();' onchange='pe.cms.settag(\"tageditor\");' title=\"" . L("Access filters") . "\" placeholder=\"" . L("Access filters") . "\" value=\"" . htmlspecialchars($acl) . "\" list='filters'>"; echo "<datalist id='filters'>"; foreach (ClassMap::ace() as $b) { echo "<option value='" . $b . "'>" . L($b) . "</option>"; } echo "<option value='siteadm|webadm'>" . L("Administrator") . "</option>"; echo "</datalist><br/>\n"; $c = @$list["_" . $widget]; } if (empty($c) || @$item[2] == "/") { die(L("Not configurable")); } if ($c[0] == "*") { $c = substr($c, 1); } $c = str_getcsv(preg_replace("/[\\ ]+/", " ", strtr($c, ["(" => "( ", ")" => " ) ", "[" => " [ ", "]" => "", "," => " "])), " "); if (in_array(")", $c)) { if (!in_array(")", $a)) { array_unshift($a, ")"); } array_shift($c); } if ($c[0] != ")" && in_array(")", $a)) { echo "(<input type='hidden' value='('><br/><div style='padding-left:10px;'>"; } elseif ($c[0] == ")") { array_shift($c); } $i = 0; $optional = ""; $f = 1; $js = 0; foreach ($c as $k => $v) { if ($v == "[") { $optional = " optional"; continue; } if ($v == ")") { echo "</div>)<input type='hidden' value=')'><br/>\n"; while ($a[$i] != ")" && !empty($a[$i])) { $i++; } $i++; $optional = ""; continue; } if (empty($optional) && $f) { $optional = " focus"; $f = 0; } switch ($v) { case "": $i++; break; case "view": $views = Views::find([], "sitebuild=''", "id", "id,name"); foreach (array_merge(glob("app/views/*.tpl"), glob("vendor/phppe/Core/views/*.tpl")) as $view) { $w = str_replace(".tpl", "", basename($view)); if ($w != "frame") { $views[] = ['id' => $w, 'name' => ucfirst($w)]; } } if ($a[$i] == ")") { $i--; } echo "<select class='input" . $optional . "' name='arg" . $k . "' data-type='" . htmlspecialchars($v) . "' " . "onchange='pe.cms.settag(\"tageditor\");' title=\"" . L($v) . "\">"; $w = 0; foreach ($views as $view) { echo "<option value='" . htmlspecialchars($view['id']) . "'" . ($view['id'] == $a[$i] ? " selected" : "") . ">" . L($view['name'] ? $view['name'] : $view['id']) . "</option>"; if ($view['id'] == $a[$i]) { $w = 1; } } if (!$w) { echo "<option value='" . htmlspecialchars($a[$i]) . "' selected>" . (!empty($a[$i]) ? L($a[$i]) : "*") . "</option>"; } echo "</select>\n"; $i++; break; case "min": case "max": case "maxlen": case "rows": case "size": case "picturesize": case "iconheight": case "iconwidth": case "itemheight": case "itemwidth": case "num": echo "<input type='number' class='input" . $optional . "' name='arg" . $k . "' data-type='" . htmlspecialchars($v) . "' " . "onkeyup='pe.cms.settag(\"tageditor\");' onkeydown='if(event.key==\"Enter\"){event.preventDefault();pe_p();}' onchange='pe.cms.settag(\"tageditor\");' title=\"" . L($v) . "\" placeholder=\"" . L($v) . "\" " . "value=\"" . htmlspecialchars(@$a[$i] == ")" ? "" : @$a[$i++]) . "\"><br/>\n"; break; default: echo "<input type='text' class='input" . $optional . "' name='arg" . $k . "' data-type='" . htmlspecialchars($v) . "' " . "onkeyup='pe.cms.settag(\"tageditor\");' onkeydown='if(event.key==\"Enter\"){event.preventDefault();pe_p();}' onchange='pe.cms.settag(\"tageditor\");' title=\"" . L($v) . "\" placeholder=\"" . L($v) . "\" " . "value=\"" . htmlspecialchars(@$a[$i] == ")" ? "" : @$a[$i++]) . "\"" . ($v == "label" || $v == "cssclass" || $v == "dataset" || $v == "listopts" ? " list=\"" . ($v == "listopts" ? "dataset" : $v) . "s\"" : (substr($v, -2) == "js" ? " list='jss'" : "")) . "><br/>\n"; if (substr($v, -2) == "js" && $js == 0) { //! filled in by JavaScript echo "<datalist id=\"jss\"></datalist>\n"; $js = 1; } if ($v == "label" || $v == "cssclass") { //! filled in by JavaScript echo "<datalist id=\"" . $v . "s\"></datalist>\n"; } if ($v == "dataset" || $v == "listopts") { echo "<datalist id=\"datasets\">\n"; $pages = Page::find([], "", "created DESC", "dds", "id"); $dds = []; foreach ($pages as $p) { $g = @json_decode(@$p['dds'], true); if (!empty($g) && is_array($g)) { foreach ($g as $G => $w) { $dds[$G] = $G; } } } ksort($dds); foreach ($dds as $G) { echo "<option value=\"" . htmlspecialchars($G) . "\">" . L($G) . "</option>"; } echo "</datalist>\n"; } } if ($optional == "focus") { $optional = ""; } } } die("</div>\n<small>" . L(!empty(Core::$l['_' . $d]) ? '_' . $d : "") . "</small>"); } else { // tag chooser $onlywidget = strpos($_SERVER['HTTP_REFERER'], "/cms/layouts/") === false; echo "<input type='text' style='width:98%;' placeholder='" . L("Search") . "' onkeyup='pe.wyswyg.search(this,this.nextSibling);'>"; echo "<div class='wyswyg_tag wyswyg_scroll'>\n"; foreach ($list as $tag => $cfg) { if ($cfg[0] == '*' && $onlywidget) { continue; } if (substr($tag, 0, 1) == "_") { $tag = ($onlywidget ? "widget" : "field") . " " . substr($tag, 1); } else { if (substr($tag, 0, 1) == "/") { echo "<img class='wyswyg_icon' src='js/wyswyg.js.php?item=" . urlencode("<!" . substr($tag, 1) . ">") . "' alt=\"" . strtr("<!" . substr($tag, 1) . ">", ["<" => "<", ">" => ">", "\"" => """]) . "\">\n"; } } echo "<img class='wyswyg_icon' src='js/wyswyg.js.php?item=" . urlencode("<!" . $tag . ">") . "' alt=\"" . strtr("<!" . $tag . ">", ["<" => "<", ">" => ">", "\"" => """]) . "\">\n"; } die("</div>"); } }
public function testEmail() { if (!\PHPPE\ClassMap::has("PHPPE\\Email")) { $this->markTestSkipped(); } \PHPPE\Core::$core->mailer = null; $email = new \PHPPE\Email(); $emailData = $email->get(); $email2 = new \PHPPE\Email($emailData); $this->assertNotEmpty($email2, "Empty Email dump"); $wasExc = false; try { $email3 = new \PHPPE\Email("something"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Email creating exception"); $wasExc = false; try { $email->send(); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No backend exception"); $wasExc = false; try { $email->send("db"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No message exception"); $email->message("Something"); $wasExc = false; try { $email->send("db"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No subject exception"); $email->subject("Subject"); \PHPPE\DS::close(); $wasExc = false; try { $email->send("db"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No recipient exception"); $wasExc = false; try { $email->to("me"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Bad address exception #1"); $wasExc = false; try { $email->to("me@notld"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "Bad address exception #2"); $email->to("me@localhost"); $email->replyTo("me2@localhost"); $email->cc("me3@localhost"); $email->bcc("me4@localhost"); $wasExc = false; try { $email->send("db"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No db exception"); $wasExc = false; try { $email->send("phpmailer"); } catch (\Exception $e) { $wasExc = true; } $this->assertTrue($wasExc, "No phpmailer exception"); $this->assertNotEquals(0, preg_match("/Message\\-ID/", $email->send("mime")), "Return mime message"); $email->attachFile("images/phppe.png"); $email->attachFile("images/phppe.png", "image/png"); $email2->attachData("something.txt", "text/plain", "something"); $mime = $email2->message("<html><body>html mail<img src='http://localhost/something.jpg'><img src='images/phppe.png'></body></html>")->subject("Subject")->to("me@localhost")->send("mime"); $this->assertTrue($email2->send("log"), "Log backend"); $email2->send("mail"); $email2->send("sendmail"); $email2->send("smtp://*****:*****@localhost")->subject("Subject")->message("message"); \PHPPE\DS::db("sqlite::memory:"); $wasExc = false; try { $email->send("db"); $email3->send("db"); } catch (\Exception $e) { $wasExc = true; echo $e; } $this->assertFalse($wasExc, "To db queue"); \PHPPE\Core::$core->realmailer = "log"; $email->cronMinute(""); \PHPPE\Core::$core->realmailer = "log"; $email->cronMinute(""); }