PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/core/Forms/bootstrap.php

https://github.com/arjunmenon/cockpit
PHP | 164 lines | 104 code | 53 blank | 7 comment | 21 complexity | 0e8fd14067a601a61fb7fbed5eda031f MD5 | raw file
  1. <?php
  2. // API
  3. $app->bind("/api/forms/submit/:form", function($params) use($app){
  4. $form = $params["form"];
  5. // Security check
  6. if ($formhash = $app->param("__csrf", false)) {
  7. if ($formhash != $app->hash($form)) {
  8. return false;
  9. }
  10. } else {
  11. return false;
  12. }
  13. $frm = $app->db->findOne("common/forms", ["name"=>$form]);
  14. if (!$frm) {
  15. return false;
  16. }
  17. if ($formdata = $app->param("form", false)) {
  18. // custom form validation
  19. if ($app->path("custom:forms/{$form}.php") && false===include($app->path("custom:forms/{$form}.php"))) {
  20. return false;
  21. }
  22. if(isset($frm["email"])) {
  23. $emails = array_map('trim', explode(',', $frm['email']));
  24. $filtered_emails = [];
  25. foreach($emails as $to){
  26. // Validate each email address individually, push if valid
  27. if(filter_var($to, FILTER_VALIDATE_EMAIL)){
  28. $filtered_emails[] = $to;
  29. }
  30. }
  31. if (count($filtered_emails)) {
  32. $frm['email'] = implode(',', $filtered_emails);
  33. $body = [];
  34. foreach ($formdata as $key => $value) {
  35. $body[] = "<b>{$key}:</b>\n<br>";
  36. $body[] = (is_string($value) ? $value:json_encode($value))."\n<br>";
  37. }
  38. $app->mailer->mail($frm["email"], $app->param("__mailsubject", "New form data for: ".$form), implode("\n<br>", $body));
  39. }
  40. }
  41. if (isset($frm["entry"]) && $frm["entry"]) {
  42. $collection = "form".$frm["_id"];
  43. $entry = ["data" => $formdata, "created"=>time()];
  44. $app->db->insert("forms/{$collection}", $entry);
  45. }
  46. return json_encode($formdata);
  47. } else {
  48. return "false";
  49. }
  50. });
  51. $this->module("forms")->extend([
  52. "form" => function($name, $options = []) use($app) {
  53. $options = array_merge(array(
  54. "id" => uniqid("form"),
  55. "class" => "",
  56. "csrf" => $app->hash($name)
  57. ), $options);
  58. $app->renderView("forms:views/api/form.php", compact('name', 'options'));
  59. },
  60. "collectionById" => function($formId) use($app) {
  61. $entrydb = "form{$formId}";
  62. return $app->db->getCollection("forms/{$entrydb}");
  63. },
  64. "entries" => function($name) use($app) {
  65. $frm = $app->db->findOne("common/forms", ["name"=>$name]);
  66. if (!$frm) {
  67. return false;
  68. }
  69. $entrydb = "form".$frm["_id"];
  70. return $app->db->getCollection("forms/{$entrydb}");
  71. }
  72. ]);
  73. if (!function_exists('form')) {
  74. function form($name, $options = []) {
  75. cockpit("forms")->form($name, $options);
  76. }
  77. }
  78. // ADMIN
  79. if(COCKPIT_ADMIN && !COCKPIT_REST) {
  80. $app->on("admin.init", function() use($app){
  81. if(!$app->module("auth")->hasaccess("Forms", ['manage.forms', 'manage.entries'])) return;
  82. $app->bindClass("Forms\\Controller\\Forms", "forms");
  83. $app->bindClass("Forms\\Controller\\Api", "api/forms");
  84. $app("admin")->menu("top", [
  85. "url" => $app->routeUrl("/forms"),
  86. "label" => '<i class="uk-icon-inbox"></i>',
  87. "title" => $app("i18n")->get("Forms"),
  88. "active" => (strpos($app["route"], '/forms') === 0)
  89. ], 5);
  90. // handle global search request
  91. $app->on("cockpit.globalsearch", function($search, $list) use($app){
  92. foreach ($app->db->find("common/forms") as $f) {
  93. if(stripos($f["name"], $search)!==false){
  94. $list[] = [
  95. "title" => '<i class="uk-icon-inbox"></i> '.$f["name"],
  96. "url" => $app->routeUrl('/forms/form/'.$f["_id"])
  97. ];
  98. }
  99. }
  100. });
  101. });
  102. $app->on("admin.dashboard.aside", function() use($app){
  103. if(!$app->module("auth")->hasaccess("Forms", ['manage.forms', 'manage.entries'])) return;
  104. $title = $app("i18n")->get("Forms");
  105. $badge = $app->db->getCollection("common/forms")->count();
  106. $forms = $app->db->find("common/forms", ["limit"=> 3, "sort"=>["created"=>-1] ])->toArray();
  107. $app->renderView("forms:views/dashboard.php with cockpit:views/layouts/dashboard.widget.php", compact('title', 'badge', 'forms'));
  108. });
  109. // acl
  110. $app("acl")->addResource("Forms", ['manage.forms', 'manage.entries']);
  111. }