PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/data.php

https://github.com/MilkZoft/zan
PHP | 148 lines | 148 code | 0 blank | 0 comment | 1 complexity | 4a2a4149c943efba622e91c9278114ea MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_Data extends ZP_Load
  6. {
  7. public function __construct()
  8. {
  9. $this->Db = $this->core("Db");
  10. $this->ignore = array("save", "edit", "_table", "_hide", "_options", "ID");
  11. $this->rename = true;
  12. }
  13. public function ignore($field = false)
  14. {
  15. if (is_array($field)) {
  16. for ($i = 0; $i <= count($field) - 1; $i++) {
  17. $this->ignore[] = $field[$i];
  18. }
  19. } elseif (is_string($field)) {
  20. $this->ignore[] = $field;
  21. }
  22. }
  23. public function process($data = null, $validations = false, $decode = true)
  24. {
  25. if (is_array($validations)) {
  26. foreach ($validations as $field => $validation) {
  27. if ($validation === "required") {
  28. if (!POST($field)) {
  29. $field = $this->rename($field);
  30. return array("error" => getAlert(__("$field is required")));
  31. }
  32. } elseif ($validation === "name?") {
  33. if (!isName(POST($field))) {
  34. return array("error" => getAlert(__("$field is not a valid name")));
  35. }
  36. } elseif ($validation === "email?") {
  37. if (!isEmail(POST($field))) {
  38. return array("error" => getAlert(__("$field is not a valid email")));
  39. }
  40. } elseif ($validation === "captcha?") {
  41. if (!POST("captcha_token") or !POST("captcha_type")) {
  42. return array("error" => getAlert(__(POST("captcha_type") === "aritmethic" ? "Please enter your answer again" : "Please type the characters you see in the picture")));
  43. } elseif (POST("captcha_type") === "aritmethic") {
  44. if (SESSION("ZanCaptcha". POST("captcha_token")) != POST($field)) {
  45. return array("error" => getAlert(__("Your answer was incorrect")));
  46. }
  47. } else {
  48. if (SESSION("ZanCaptcha". POST("captcha_token")) !== POST($field)) {
  49. return array("error" => getAlert(__("The characters did not match the picture")));
  50. }
  51. }
  52. } elseif ($validation === "injection?") {
  53. if (isInjection(POST($field))) {
  54. return array("error" => getAlert(__("SQL/HTML injection attempt blocked")));
  55. }
  56. } elseif ($validation === "spam?") {
  57. if (isSPAM(POST($field))) {
  58. return array("error" => getAlert(__("SPAM prohibited")));
  59. }
  60. } elseif ($validation === "vulgar?") {
  61. if (isVulgar(POST($field))) {
  62. return array("error" => getAlert(__("Your $field is very vulgar")));
  63. }
  64. } elseif ($validation === "ping") {
  65. if (!ping(POST($field))) {
  66. return array("error" => getAlert(__("Invalid URL")));
  67. }
  68. } elseif (is_string($validation) and substr($validation, 0, 6) === "length") {
  69. $count = (int) substr($validation, 7, 8);
  70. $count = ($count > 0) ? $count : 6;
  71. if (strlen(POST($field)) < $count) {
  72. return array("error" => getAlert( __("$field")." ".__("must have at least")." $count ".__("characters")));
  73. }
  74. } elseif (isset($field["exists"]) and isset($this->table)) {
  75. if (is_array($validation)) {
  76. if (isset($validation["or"]) and count($validation) > 2) {
  77. unset($validation["or"]);
  78. $fields = array_keys($validation);
  79. for ($i = 0; $i <= count($fields) - 1; $i++) {
  80. $exists = $this->Db->findBy($fields[$i], $validation[$fields[$i]]);
  81. if ($exists) {
  82. return array("error" => getAlert(__("The ". strtolower($fields[$i]) ." already exists")));
  83. }
  84. }
  85. } else {
  86. $field = array_keys($validation);
  87. $exists = $this->Db->findBy($field[0], $validation[$field[0]]);
  88. if ($exists) {
  89. return array("error" => getAlert(__("The ". strtolower($field[0]) ." already exists")));
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. if (is_null($data)) {
  97. $data = array();
  98. }
  99. $POST = POST(true);
  100. foreach ($POST as $field => $value) {
  101. if (!in_array($field, $this->ignore)) {
  102. if (!isset($data[$this->rename($field)])) {
  103. $data[$this->rename($field)] = ($decode) ? decode(filter($value, "escape")) : filter($value, "escape");
  104. }
  105. }
  106. }
  107. return $data;
  108. }
  109. public function change($field, $newField)
  110. {
  111. $this->changes[$field] = $newField;
  112. }
  113. public function rename($field)
  114. {
  115. if ($this->rename) {
  116. if (isset($this->changes[$field])) {
  117. $field = $this->changes[$field];
  118. }
  119. $field = str_replace("_", " ", $field);
  120. $field = ucwords($field);
  121. $field = str_replace(" ", "_", $field);
  122. }
  123. return $field;
  124. }
  125. public function table($table)
  126. {
  127. $this->table = $table;
  128. $this->Db->table($this->table);
  129. }
  130. }