├── assets
└── .keep
├── app
├── models
│ └── .keep
├── services
│ └── .keep
├── controllers
│ └── TestController.php
└── views
│ └── error
│ ├── 404.view.twig
│ ├── 403.view.twig
│ └── 500.view.twig
├── .gitignore
├── .github
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ └── custom.md
├── dependabot.yml
└── workflows
│ ├── versionChecker.yml
│ └── snyk-container.yml
├── core
├── logger
│ ├── Logger.php
│ └── LogFile.php
├── router
│ ├── Request.php
│ ├── App.php
│ └── Router.php
├── lib
│ ├── Validate.php
│ ├── Response.php
│ ├── Email.php
│ ├── AuthHead.php
│ └── emailValidator
│ │ └── Validator.php
├── inc
│ ├── bootstrap.php
│ ├── config.php
│ ├── DotEnv.php
│ └── helpers.php
└── database
│ ├── Connection.php
│ ├── Model.php
│ └── QueryBuilder.php
├── bloc
└── routes.php
├── .env.example
├── .htaccess
├── Dockerfile
├── index.php
├── composer.json
├── LICENSE.md
├── README.md
└── CODE_OF_CONDUCT.md
/assets/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/models/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/services/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | vendor
2 | logs
3 | .idea
4 | .env
5 | /composer.lock
6 | uploads
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [rejinsha]
4 |
5 | custom: ['https://ko-fi.com/rejin']
6 |
--------------------------------------------------------------------------------
/core/logger/Logger.php:
--------------------------------------------------------------------------------
1 | getArray([
9 | 'test' => 'TestController@test'
10 | #'rest/api/test/{id}' => 'TestController@test'
11 | ]);
12 |
13 | $router->postArray([
14 | 'rest/api/test' => 'TestController@test'
15 | ]);
16 |
--------------------------------------------------------------------------------
/.env.example:
--------------------------------------------------------------------------------
1 | APP_ENV=dev
2 | DATABASE_HOST=
3 | DATABASE_NAME=
4 | DATABASE_USERNAME=
5 | DATABASE_PASSWORD=
6 | DEBUG=true
7 | PRODUCTION=false
8 | ARRAY_ROUTING=false
9 | SECRET=
10 | SEVER_NAME=
11 |
12 |
13 | # Email Service
14 | HOST =
15 | USERNAME =
16 | PASSWORD =
17 | FROM_EMAIL =
18 | FROM_NAME =
19 | REPLY_TO =
20 | REPLY_NAME =
21 | SMTP_TYPE = ssl
--------------------------------------------------------------------------------
/core/router/Request.php:
--------------------------------------------------------------------------------
1 |
21 |
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 |
2 | RewriteEngine On
3 |
4 | RewriteCond %{REQUEST_FILENAME} !-d
5 | RewriteCond %{REQUEST_URI} (.+)/$
6 | RewriteRule ^ %1 [L,R=301]
7 |
8 | RewriteCond %{REQUEST_FILENAME} !-d
9 | RewriteCond %{REQUEST_FILENAME} !-f
10 | RewriteRule ^ index.php [L]
11 |
12 | RewriteCond %{SERVER_PORT} ^80$
13 | RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
14 |
15 |
--------------------------------------------------------------------------------
/core/lib/Validate.php:
--------------------------------------------------------------------------------
1 |
27 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM php:7-apache
2 |
3 | RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql && pecl install apcu && docker-php-ext-enable apcu
4 |
5 | RUN docker-php-ext-install opcache && docker-php-ext-enable opcache
6 |
7 | RUN apt update && apt install -y zlib1g-dev libpng-dev g++ libicu-dev libpq-dev libzip-dev zip zlib1g-dev
8 |
9 | RUN docker-php-ext-install gd && docker-php-ext-enable gd
10 |
11 | RUN a2enmod rewrite
12 |
13 | RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
14 |
15 | COPY . /var/www/html/
16 |
17 | RUN chmod -R a+r /var/www/html/
18 |
19 | RUN composer install
20 |
21 | EXPOSE 80
22 |
--------------------------------------------------------------------------------
/core/inc/config.php:
--------------------------------------------------------------------------------
1 | load();
8 |
9 | return [
10 | 'database' => [
11 | 'name' => getenv('DATABASE_NAME'),
12 | 'username' => getenv('DATABASE_USERNAME'),
13 | 'password' => getenv('DATABASE_PASSWORD'),
14 | 'connection' => 'mysql:host=' . getenv('DATABASE_HOST'),
15 | 'options' => [
16 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
17 | PDO::ATTR_CASE => PDO::CASE_NATURAL
18 | ]
19 | ],
20 | 'options' => [
21 | 'debug' => getenv('DEBUG'),
22 | 'production' => getenv('PRODUCTION'),
23 | 'array_routing' => getenv('ARRAY_ROUTING')
24 | ]
25 | ];
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | direct(Request::uri(), Request::method());
27 | } catch (Exception $e) {
28 | return $e;
29 | }
30 |
--------------------------------------------------------------------------------
/core/logger/LogFile.php:
--------------------------------------------------------------------------------
1 | log($data, "info.log");
15 | }
16 |
17 | private function log($data, $filename = "log.log")
18 | {
19 | if (!file_exists("logs/") && (!mkdir("logs/", 0777, true) && !is_dir($filename))) {
20 | throw new RuntimeException(sprintf('Folder "%s" was not created', $filename));
21 | }
22 | return file_put_contents("logs/" . $filename, date("Y-m-d h:i:sa") . " " . $data . "\n", FILE_APPEND);
23 | }
24 |
25 | public function error($data)
26 | {
27 | return $this->log($data, "error.log");
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/core/lib/Response.php:
--------------------------------------------------------------------------------
1 | "$errorCode",
30 | "message" => "$message"
31 | ]
32 | );
33 |
34 | }
35 |
36 | }
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "miyaave/miyaave",
3 | "type": "project",
4 | "description": "The MiyaaVe Framework.",
5 | "keywords": [
6 | "framework",
7 | "miyaave"
8 | ],
9 | "license": "MIT",
10 | "autoload": {
11 | "classmap": [
12 | "./"
13 | ]
14 | },
15 | "require": {
16 | "php": ">=5.4",
17 | "firebase/php-jwt": "^6.0",
18 | "php-curl-class/php-curl-class": "^9.5",
19 | "phpmailer/phpmailer": "^6.5",
20 | "twig/twig": "^3.0",
21 | "ext-pdo": "*"
22 | },
23 | "require-dev": {
24 | "overtrue/phplint": "3.0",
25 | "phpunit/phpunit": "^9.5.10"
26 | },
27 | "scripts": {
28 | "post-root-package-install": [
29 | "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
30 | ]
31 | },
32 | "extra": {
33 | "miyaave": {
34 | "dont-discover": []
35 | }
36 | },
37 | "config": {
38 | "optimize-autoloader": true,
39 | "preferred-install": "dist",
40 | "sort-packages": true
41 | },
42 | "minimum-stability": "dev",
43 | "prefer-stable": true
44 | }
45 |
--------------------------------------------------------------------------------
/core/database/Connection.php:
--------------------------------------------------------------------------------
1 | $e->getMessage()]);
28 | }
29 | header('HTTP/1.0 500 PDO Exception Con');
30 | return iView('error/500');
31 | }
32 | }
33 | }
34 |
35 |
36 | ?>
37 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021-2022 Miyaa Ve
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/core/inc/DotEnv.php:
--------------------------------------------------------------------------------
1 | path = $path;
19 | }
20 |
21 | public function load()
22 | {
23 | if (!is_readable($this->path)) {
24 | throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
25 | }
26 |
27 | $lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
28 | foreach ($lines as $line) {
29 |
30 | if (strpos(trim($line), '#') === 0) {
31 | continue;
32 | }
33 |
34 | list($name, $value) = explode('=', $line, 2);
35 | $name = trim($name);
36 | $value = trim($value);
37 |
38 | if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
39 | putenv(sprintf('%s=%s', $name, $value));
40 | $_ENV[$name] = $value;
41 | $_SERVER[$name] = $value;
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core/lib/Email.php:
--------------------------------------------------------------------------------
1 | isSMTP();
20 | $SMTP->SMTPDebug = false;
21 | $SMTP->Host = getenv('HOST');
22 | $SMTP->SMTPAuth = true;
23 | $SMTP->Username = getenv('USERNAME');
24 | $SMTP->Password = getenv('PASSWORD');
25 | $SMTP->SMTPSecure = getenv('SMTP_TYPE');
26 | $SMTP->Port = 465;
27 |
28 | $SMTP->setFrom(getenv('FROM_EMAIL'), getenv('FROM_NAME'));
29 | $SMTP->addAddress($to, $name);
30 | $SMTP->addReplyTo(getenv('REPLY_TO'), getenv('REPLY_NAME'));
31 |
32 | $SMTP->isHTML(true);
33 | $SMTP->Subject = $subject;
34 | $SMTP->Body = $message;
35 | $SMTP->send();
36 | return true;
37 | } catch (Exception $e) {
38 |
39 | if (App::get('config')['options']['debug']) {
40 | App::logError('There was a PDO Exception. Details: ' . $e);
41 | return false;
42 | }
43 | return false;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/core/router/App.php:
--------------------------------------------------------------------------------
1 | info($data);
51 | }
52 |
53 | public static function logError($data, Logger $logger = null)
54 | {
55 | $logger = $logger ?: new LogFile();
56 | return $logger->error($data);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/core/lib/AuthHead.php:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # Miyaave
4 |
5 | [](https://packagist.org/packages/miyaave/miyaave) [](https://packagist.org/packages/miyaave/miyaave) [](https://packagist.org/packages/miyaave/miyaave)
6 | [](https://packagist.org/packages/miyaave/miyaave)
7 | [](https://packagist.org/packages/miyaave/miyaave)
8 | [](https://packagist.org/packages/miyaave/miyaave) [](https://packagist.org/packages/miyaave/miyaave)
9 |
10 |
11 | ## About Miyaave
12 |
13 | Miyaave is a single backend to integrate with both web and mobile application . It helps to reduce the time & development and also fast , simple stepups . We believe that development should be an enjoyable and constructive experience to be truly fulfilling .
14 |
15 | Miyaaveis suitable for both small to large application .
16 |
17 | ## Server Requirements
18 |
19 | PHP version 7.4 or higher is required, with the following extensions installed:
20 |
21 | - intl
22 | - libcurl if you plan to use the HTTP\CURLRequest library
23 | - mbstring
24 |
25 | ## Installing Miyaave via Composer
26 |
27 | You can install Miyaave into your project using Composer.
28 |
29 | 1. Download [Composer](http://getcomposer.org/doc/00-intro.md) or update `composer self-update`.
30 | 2. Run `composer create-project miyaave/miyaave`.
31 |
32 | If Composer is installed globally, run
33 |
34 | ```bash
35 | composer create-project miyaave/miyaave
36 | ```
37 |
38 | You should now be able to visit the path to where you installed the app and see
39 | the setup traffic lights.
40 |
41 | ## Learning Miyaave
42 |
43 | Miyaave has the most comprehensive and complete [documentation](https://www.miyaave.com/docs) .
44 |
45 | ## Authors
46 |
47 | - Rejinsha Shahudeen | [GitHub](https://github.com/Rejinsha) | [Rejinshalb@gmail.com](mailto:Rejinshalb@gmail.com)
48 | - Priya Subramanian | [GitHub](https://github.com/priyasubramanian21) | [priya162125@gmail.com](mailto:priya162125@gmail.com)
49 |
50 | ## Security Vulnerabilities
51 |
52 | Please send any sensitive issue to [support@miyaave.com](mailto:support@miyaave.com). Thanks!
53 |
54 | ## License
55 |
56 | The Miyaave is licensed under the [MIT license](https://opensource.org/licenses/MIT).
57 |
--------------------------------------------------------------------------------
/core/lib/emailValidator/Validator.php:
--------------------------------------------------------------------------------
1 | isEmail($email)) {
28 | return false;
29 | }
30 |
31 |
32 | if ($this->isExample($email)) {
33 | return false;
34 | }
35 |
36 |
37 | if ($this->isRole($email)) {
38 | return false;
39 | }
40 |
41 |
42 | if (!$this->hasMx($email)) {
43 | return false;
44 | }
45 |
46 | return true;
47 | }
48 |
49 | public static function isEmail($email)
50 | {
51 | if (is_string($email)) {
52 | return (bool)preg_match('/^.+@.+\..+$/i', $email);
53 | }
54 |
55 | return false;
56 | }
57 |
58 | public function isExample($email)
59 | {
60 | if (!$this->isEmail($email)) {
61 | return null;
62 | }
63 |
64 | $hostname = $this->hostnameFromEmail($email);
65 |
66 | if ($hostname) {
67 | if (in_array($hostname, $this->exampleDomains)) {
68 | return true;
69 | }
70 |
71 | foreach ($this->exampleTlds as $tld) {
72 | $length = strlen($tld);
73 | $subStr = substr($hostname, -$length);
74 |
75 | if ($subStr == $tld) {
76 | return true;
77 | }
78 | }
79 |
80 | return false;
81 | }
82 |
83 | return null;
84 | }
85 |
86 | private function hostnameFromEmail($email)
87 | {
88 | $parts = explode('@', $email);
89 |
90 | if (count($parts) == 2) {
91 | return strtolower($parts[1]);
92 | }
93 |
94 | return null;
95 | }
96 |
97 | public function isRole($email)
98 | {
99 | if (!$this->isEmail($email)) {
100 | return null;
101 | }
102 |
103 | $user = $this->userFromEmail($email);
104 |
105 | if ($user) {
106 |
107 | // Search array for hostname
108 | if (in_array($user, $this->role)) {
109 | return true;
110 | }
111 |
112 | return false;
113 | }
114 |
115 | return null;
116 | }
117 |
118 | private function userFromEmail($email)
119 | {
120 | $parts = explode('@', $email);
121 |
122 | if (count($parts) == 2) {
123 | return strtolower($parts[0]);
124 | }
125 |
126 | return null;
127 | }
128 |
129 | public function hasMx($email)
130 | {
131 | if (!$this->isEmail($email)) {
132 | return null;
133 | }
134 |
135 | $hostname = $this->hostnameFromEmail($email);
136 |
137 | if ($hostname) {
138 | return checkdnsrr($hostname, 'MX');
139 | }
140 |
141 | return null;
142 | }
143 |
144 | public function isSendable($email)
145 | {
146 |
147 | if (!$this->isEmail($email)) {
148 | return false;
149 | }
150 |
151 | if ($this->isExample($email)) {
152 | return false;
153 | }
154 |
155 | if (!$this->hasMx($email)) {
156 | return false;
157 | }
158 |
159 | return true;
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/core/inc/helpers.php:
--------------------------------------------------------------------------------
1 | true,
33 | ]);
34 | $twig->addExtension(new DebugExtension());
35 |
36 | if (!file_exists("$path{$name}$ext")) {
37 | header('HTTP/1.0 404 Not Found');
38 | return iView("error/404");
39 | } else {
40 | try {
41 | return $twig->render("{$name}$ext", $data);
42 | } catch (LoaderError $e) {
43 | App::logError('There was a Twig LoaderError Exception. Details: ' . $e);
44 | header('HTTP/1.0 404 Twig LoaderError');
45 | return iView("error/404", ['error' => $e]);
46 | } catch (RuntimeError $e) {
47 | App::logError('There was a Twig RuntimeError Exception. Details: ' . $e);
48 | header('HTTP/1.0 404 wig RuntimeError');
49 | return iView("error/404", ['error' => $e]);
50 | } catch (SyntaxError $e) {
51 | App::logError('There was a Twig SyntaxError Exception. Details: ' . $e);
52 | header('HTTP/1.0 404 Twig SyntaxError');
53 | return iView("error/404", ['error' => $e]);
54 | }
55 | }
56 | }
57 |
58 |
59 | function iView($name, $data = [])
60 | {
61 | extract($data);
62 | return require "app/views/{$name}.view.twig";
63 | }
64 |
65 | /*
66 | * This function is used for dark mode functionality,
67 | * it returns the first (dark) class string
68 | * or second (light class string).
69 | */
70 | function theme($class, $secondClass)
71 | {
72 | if (isset($_SESSION['darkmode']) && $_SESSION['darkmode'] == true) {
73 | return $class;
74 | }
75 | return $secondClass;
76 | }
77 |
78 | /*
79 | * This function is used for dying and dumping.
80 | */
81 | function dd($value)
82 | {
83 | echo "";
84 | var_dump($value);
85 | echo "
";
86 | }
87 |
88 | /*
89 | * This function is used for generating pagination links.
90 | */
91 | function paginate($table, $page, $limit, $count)
92 | {
93 | $offset = ($page - 1) * $limit;
94 | $output = "";
95 | if ($page > 1) {
96 | $prev = $page - 1;
97 | $output .= "Prev";
98 | }
99 | $output .= " Page $page ";
100 | if ($count > ($offset + $limit)) {
101 | $next = $page + 1;
102 | $output .= "Next";
103 | }
104 | $output .= "";
105 | return $output;
106 | }
107 |
108 | /*
109 | * This function displays a session variable's value if it exists.
110 | */
111 | function session($name)
112 | {
113 | return $_SESSION[$name] ?? "";
114 | }
115 |
116 | /*
117 | * This function displays a session variable's value and unsets it if it exists.
118 | */
119 | function session_once($name)
120 | {
121 | if (isset($_SESSION[$name])) {
122 | $value = $_SESSION[$name];
123 | unset($_SESSION[$name]);
124 | return $value;
125 | }
126 | return "";
127 | }
128 |
129 | /*
130 | * This function enables displaying of errors in the web browser.
131 | */
132 | function display_errors()
133 | {
134 | ini_set('display_errors', 1);
135 | ini_set('display_startup_errors', 1);
136 | error_reporting(E_ALL);
137 | }
138 |
--------------------------------------------------------------------------------
/core/router/Router.php:
--------------------------------------------------------------------------------
1 | [],
15 | 'POST' => []
16 |
17 | ];
18 |
19 | /*
20 | * This function loads the routes from a file. In this framework, the routes are stored in app/routes.php.
21 | */
22 | public static function load($file)
23 | {
24 | $router = new static;
25 |
26 | require $file;
27 |
28 | return $router;
29 | }
30 |
31 | /*
32 | * This function gets the GET route based on the URI and passes it off to the controller.
33 | */
34 | public function get($uri, $controller)
35 | {
36 | $this->routes['GET'][$uri] = $controller;
37 | }
38 |
39 | /*
40 | * This function gets the POST route based on the URI and passes it off to the controller.
41 | */
42 | public function post($uri, $controller)
43 | {
44 | $this->routes['POST'][$uri] = $controller;
45 | }
46 |
47 | /*
48 | * This function using array notation routing gets the GET routes. PHP does not support function overloading (also known as method overloading in OOP), so we cannot name this function get even though it has a different number of parameters than the get function used for routing without array notation.
49 | */
50 | public function getArray($routes)
51 | {
52 | $this->routes['GET'] = $routes;
53 | }
54 |
55 | /*
56 | * This function using array notation routing gets the POST routes. PHP does not support function overloading (also known as method overloading in OOP), so we cannot name this function post even though it has a different number of parameters than the post function used for routing without array notation.
57 | */
58 | public function postArray($routes)
59 | {
60 | $this->routes['POST'] = $routes;
61 | }
62 |
63 | /*
64 | * This function directs the user to the route based on the request type.
65 | */
66 | public function direct($uri, $requestType)
67 | {
68 | if (array_key_exists($uri, $this->routes[$requestType])) {
69 | try {
70 | return $this->callAction(
71 | ...explode('@', $this->routes[$requestType][$uri])
72 | );
73 | } catch (Exception $e) {
74 | header('HTTP/1.0 404 Unauthorized');
75 | return iView('error/404');
76 | }
77 | }
78 |
79 | foreach ($this->routes[$requestType] as $key => $value) {
80 | $pattern = preg_replace('#\(/\)#', '/?', $key);
81 | //$pattern = str_replace(".", "", $pattern);
82 | $pattern = "@^" . preg_replace('/{([\w\-]+)}/', '(?<$1>[\w\-]+)', $pattern) . "$@D";
83 | preg_match($pattern, $uri, $matches);
84 | array_shift($matches);
85 | if ($matches) {
86 | $action = explode('@', $value);
87 | try {
88 | return $this->callAction($action[0], $action[1], $matches);
89 | } catch (Exception $e) {
90 | header('HTTP/1.0 404 Not Found');
91 | return iView('error/404');
92 | }
93 | }
94 | }
95 |
96 | header('HTTP/1.0 404 Not Found');
97 | return iView('error/404');
98 | //throw new Exception('No route defined for this URI.');
99 | }
100 |
101 | /*
102 | * This function calls the controller for an action.
103 | */
104 | protected function callAction($controller, $action, $vars = [])
105 | {
106 | $controller = "app\\controllers\\{$controller}";
107 |
108 | $controller = new $controller;
109 |
110 | if (!method_exists($controller, $action)) {
111 | throw new Exception("{$controller} does not respond to the {$action} action.");
112 | }
113 |
114 | return $controller->$action($vars);
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, religion, or sexual identity
10 | and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the
26 | overall community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or
31 | advances of any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email
35 | address, without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | hello@miyaave.com.
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series
86 | of actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or
93 | permanent ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within
113 | the community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.0, available at
119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120 |
121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct
122 | enforcement ladder](https://github.com/mozilla/diversity).
123 |
124 | [homepage]: https://www.contributor-covenant.org
125 |
126 | For answers to common questions about this code of conduct, see the FAQ at
127 | https://www.contributor-covenant.org/faq. Translations are available at
128 | https://www.contributor-covenant.org/translations.
129 |
--------------------------------------------------------------------------------
/app/views/error/404.view.twig:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 404
6 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
279 |
283 |
284 |
285 |
404 - Not Found
286 |
The page you are looking not found.
287 |
288 |
289 |
290 |
--------------------------------------------------------------------------------
/app/views/error/403.view.twig:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 403
6 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
279 |
283 |
284 |
285 |
403 - Forbidden
286 |
You are unauthorized to see this page.
287 |
288 |
289 |
290 |
--------------------------------------------------------------------------------
/app/views/error/500.view.twig:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 500
6 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
279 |
283 |
284 |
285 |
500 - System Error
286 |
The website is currently unavailable.
Try again later or contact the developer.
287 |
288 |
289 |
290 |
--------------------------------------------------------------------------------
/core/database/Model.php:
--------------------------------------------------------------------------------
1 | setClassName(static::class)->selectAll(static::$table);
53 | }
54 |
55 | /**
56 | * This method returns the last SQL query by the query builder.
57 | * @return string
58 | * @throws Exception
59 | */
60 | public function getSql()
61 | {
62 | return App::DB()->setClassName(get_class($this))->getSql();
63 | }
64 |
65 | public function query($sql)
66 | {
67 | return App::DB()->setClassName(get_class($this))->query($sql);
68 | }
69 |
70 | /**
71 | * This method finds one or more rows in the database based off of ID and binds it to the Model, or returns null if no rows are found.
72 | * @param $id
73 | * @return $this
74 | * @throws Exception
75 | */
76 | public function find($id)
77 | {
78 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
79 | $this->rows = App::DB()->setClassName(get_class($this))->selectAllWhere(static::$table, [[$this->cols[0]->Field, '=', $id]]);
80 | return !empty($this->rows) ? $this : null;
81 | }
82 |
83 | /**
84 | * This method finds one or more rows in the database based off of ID and binds it to the Model, or throws an exception if no rows are found.
85 | * @param $id
86 | * @return $this
87 | * @throws Exception
88 | */
89 | public function findOrFail($id)
90 | {
91 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
92 | $this->rows = App::DB()->setClassName(get_class($this))->selectAllWhere(static::$table, [[$this->cols[0]->Field, '=', $id]]);
93 | if (!empty($this->rows)) {
94 | return $this;
95 | }
96 | throw new RuntimeException("ModelNotFoundException");
97 | }
98 |
99 | /**
100 | * This method finds one or more rows matching specific criteria in the database and binds it to the Model, then returns the Model.
101 | * @param $where
102 | * @return $this
103 | * @throws Exception
104 | */
105 | public function where($where, $limit = "", $offset = "")
106 | {
107 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
108 | $this->rows = App::DB()->setClassName(get_class($this))->selectAllWhere(static::$table, $where, $limit, $offset);
109 | return $this;
110 | }
111 |
112 | public function iWhere($column, $condition)
113 | {
114 | return App::DB()->setClassName(get_class($this))->iWhere($column, static::$table, $condition);
115 | }
116 |
117 | /**
118 | * This method returns the count of the rows for a database query.
119 | * @param $where
120 | * @return int|bool
121 | * @throws Exception
122 | */
123 | public function count($where = "")
124 | {
125 | if (!empty($where)) {
126 | return App::DB()->setClassName(get_class($this))->countWhere(static::$table, $where);
127 | }
128 | return App::DB()->setClassName(get_class($this))->count(static::$table);
129 | }
130 |
131 | /**
132 | * This method adds the row to the database and binds it to the model.
133 | * @param $columns
134 | * @return $this
135 | * @throws Exception
136 | */
137 | public function add($columns)
138 | {
139 | $this->id = App::DB()->insert(static::$table, $columns);
140 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
141 | $this->rows = App::DB()->setClassName(get_class($this))->selectAllWhere(static::$table, [[$this->cols[0]->Field, '=', $this->id]]);
142 | return $this;
143 | }
144 |
145 | /**
146 | * This method updates one or more rows in the database.
147 | * @param $parameters
148 | * @return int
149 | * @throws Exception
150 | */
151 | public function update($parameters)
152 | {
153 | return App::DB()->update(static::$table, $parameters);
154 | }
155 |
156 | /**
157 | * This method updates one or more rows in the database matching specific criteria.
158 | * @param $parameters
159 | * @param $where
160 | * @return int
161 | * @throws Exception
162 | */
163 | public function updateWhere($parameters, $where)
164 | {
165 | return App::DB()->updateWhere(static::$table, $parameters, $where);
166 | }
167 |
168 | public function iUpdate($parameters, $where)
169 | {
170 |
171 | return App::DB()->iUpDate(static::$table, $parameters, $where);
172 | }
173 |
174 | /**
175 | * This method deletes one or more rows from the database.
176 | * @return int
177 | * @throws Exception
178 | */
179 | public function delete()
180 | {
181 | return App::DB()->delete(static::$table);
182 | }
183 |
184 | /**
185 | * This method deletes one or more rows from the database matching specific criteria.
186 | * @param $where
187 | * @return int
188 | * @throws Exception
189 | */
190 | public function deleteWhere($where)
191 | {
192 | return App::DB()->deleteWhere(static::$table, $where);
193 | }
194 |
195 | /**
196 | * This method updates one or more rows in the database.
197 | * @return $this
198 | * @throws Exception
199 | */
200 | public function save()
201 | {
202 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
203 | $newValues = [];
204 | foreach ($this->cols as $col) {
205 | $newValues[$col->Field] = $this->{$col->Field};
206 | }
207 | App::DB()->updateWhere(static::$table, $newValues, [[$this->cols[0]->Field, '=', $this->{$this->cols[0]->Field}]]);
208 | return $this;
209 | }
210 |
211 | /**
212 | * This method fetches all of the rows for the Model.
213 | * @return Model[]
214 | */
215 | public function get()
216 | {
217 | return $this->rows;
218 | }
219 |
220 | /**
221 | * This method fetches all of the columns for the Model.
222 | * This returns the columns if they're cached, otherwise they are fetched again first.
223 | * @return array
224 | */
225 | public function describe()
226 | {
227 | if (!$this->cols) {
228 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
229 | }
230 | return $this->cols;
231 | }
232 |
233 | /**
234 | * This method fetches the first row for the Model.
235 | * @return Model|null
236 | */
237 | public function first()
238 | {
239 | return $this->rows[0] ?: null;
240 | }
241 |
242 | /**
243 | * This method fetches the first row for the Model or throws an exception if a row is not found.
244 | * @return Model
245 | * @throws Exception
246 | */
247 | public function firstOrFail()
248 | {
249 | if (!empty($this->rows[0])) {
250 | return $this->rows[0];
251 | }
252 | throw new RuntimeException("ModelNotFoundException");
253 | }
254 |
255 | /**
256 | * This method returns the primary key's value for the Model, or null if it doesn't have one.
257 | * @return string|null
258 | * @throws Exception
259 | */
260 | public function id()
261 | {
262 | if (!$this->cols) {
263 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
264 | }
265 | return $this->{$this->cols[0]->Field} ?: null;
266 | }
267 |
268 | /**
269 | * This method returns the primary key's name for the Model, or null if it doesn't have one.
270 | * @return string|null
271 | * @throws Exception
272 | */
273 | public function primary()
274 | {
275 | if (!$this->cols) {
276 | $this->cols = App::DB()->setClassName(get_class($this))->describe(static::$table);
277 | }
278 | return $this->cols[0]->Field ?: null;
279 | }
280 | }
--------------------------------------------------------------------------------
/core/database/QueryBuilder.php:
--------------------------------------------------------------------------------
1 | pdo = $pdo;
38 | }
39 |
40 | /**
41 | * This method returns the PDO instance.
42 | * @return PDO
43 | */
44 | public function getPdo()
45 | {
46 | return $this->pdo;
47 | }
48 |
49 | /**
50 | * This method returns the last set SQL query.
51 | *
52 | */
53 | public function getSql()
54 | {
55 | return $this->sql;
56 | }
57 |
58 | /**
59 | * This method sets the class name to bind the Model to.
60 | * @param mixed $class_name
61 | * @return QueryBuilder
62 | */
63 | public function setClassName($class_name)
64 | {
65 | $this->class_name = $class_name;
66 | return $this;
67 | }
68 |
69 | /**
70 | * This method selects all of the rows from a table in a database.
71 | * @param string $table
72 | * @param string $limit
73 | * @param string $offset
74 | * @return array|false
75 | * @throws Exception
76 | */
77 | public function selectAll($table, $limit = "", $offset = "")
78 | {
79 | return $this->select($table, "*", $limit, $offset);
80 | }
81 |
82 | /**
83 | * This method selects rows from a table in a database.
84 | * @param string $table
85 | * @param string $columns
86 | * @param string $limit
87 | * @param string $offset
88 | * @return array|false
89 | * @throws Exception
90 | */
91 | public function select($table, $columns, $limit = "", $offset = "")
92 | {
93 | $limit = $this->prepareLimit($limit);
94 | $offset = $this->prepareOffset($offset);
95 | $this->sql = "SELECT {$columns} FROM {$table} {$limit} {$offset}";
96 | try {
97 | $statement = $this->pdo->prepare($this->sql);
98 | $statement->execute();
99 | return $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
100 | } catch (PDOException $e) {
101 | $this->handlePDOException($e);
102 | }
103 | return false;
104 | }
105 |
106 | /**
107 | * This method prepares the limit statement for the query builder.
108 | * @param $limit
109 | * @return string
110 | */
111 | private function prepareLimit($limit)
112 | {
113 | return (!empty($limit) ? " LIMIT " . $limit : "");
114 | }
115 |
116 | /**
117 | * This method prepares the offset statement for the query builder.
118 | * @param $offset
119 | * @return string
120 | */
121 | private function prepareOffset($offset)
122 | {
123 | return (!empty($offset) ? " OFFSET " . $offset : "");
124 | }
125 |
126 | /**
127 | * This method handles PDO exceptions.
128 | * @param PDOException $e
129 | * @return mixed
130 | * @throws Exception
131 | */
132 | private function handlePDOException(PDOException $e)
133 | {
134 | App::logError('There was a PDO Exception. Details: ' . $e);
135 | if (App::get('config')['options']['debug']) {
136 | header('HTTP/1.0 500 PDO Exception QB');
137 | return iView('error/500', ['error' => $e->getMessage()]);
138 | }
139 | header('HTTP/1.0 500 PDO Exception QB');
140 | return iView('error/500');
141 | }
142 |
143 | /**
144 | * This method returns the number of rows in a table.
145 | * @param string $sql
146 | * @return int|bool|string|array
147 | * @throws Exception
148 | */
149 | public function query($sql = "")
150 | {
151 | $this->sql = $sql;
152 |
153 | try {
154 | $statement = $this->pdo->prepare($this->sql);
155 | $statement->execute();
156 | return $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
157 | } catch (PDOException $e) {
158 | $this->handlePDOException($e);
159 | }
160 | return false;
161 | }
162 |
163 | public function iWhere($column, $table, $where = "")
164 | {
165 | $this->sql = "SELECT {$column} FROM {$table} WHERE {$where}";
166 | try {
167 | $statement = $this->pdo->prepare($this->sql);
168 | $statement->execute();
169 | return $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
170 | } catch (PDOException $e) {
171 | $this->handlePDOException($e);
172 | }
173 | return false;
174 | }
175 |
176 | /**
177 | * This method selects rows from a table in a database where one or more conditions are matched.
178 | * @param string $table
179 | * @param $where
180 | * @param string $limit
181 | * @param string $offset
182 | * @return array|false
183 | * @throws Exception
184 | */
185 | public function selectAllWhere($table, $where, $limit = "", $offset = "")
186 | {
187 | return $this->selectWhere($table, "*", $where, $limit, $offset);
188 | }
189 |
190 | /**
191 | * This method selects rows from a table in a database where one or more conditions are matched.
192 | * @param string $table
193 | * @param string $columns
194 | * @param $where
195 | * @param string $limit
196 | * @param string $offset
197 | * @return array|false
198 | * @throws Exception
199 | */
200 | public function selectWhere($table, $columns, $where, $limit = "", $offset = "")
201 | {
202 | $limit = $this->prepareLimit($limit);
203 | $offset = $this->prepareOffset($offset);
204 | $where = $this->prepareWhere($where);
205 | $mapped_wheres = $this->prepareMappedWheres($where);
206 | $where = array_column($where, 3);
207 | $this->sql = "SELECT {$columns} FROM {$table} WHERE {$mapped_wheres} {$limit} {$offset}";
208 | try {
209 | $statement = $this->pdo->prepare($this->sql);
210 | $statement->execute($where);
211 | return $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
212 | } catch (PDOException $e) {
213 | $this->handlePDOException($e);
214 | }
215 | return false;
216 | }
217 |
218 | /**
219 | * This method prepares the where clause array for the query builder.
220 | * @param $where
221 | * @return mixed
222 | */
223 | private function prepareWhere($where)
224 | {
225 | $array = $where;
226 | foreach ($where as $key => $value) {
227 | if (count($value) < 4) {
228 | array_unshift($array[$key], 0);
229 | }
230 | }
231 | return $array;
232 | }
233 |
234 | /**
235 | * This method prepares the mapped wheres.
236 | * @param $where
237 | * @return string
238 | */
239 | private function prepareMappedWheres($where)
240 | {
241 | $mapped_wheres = '';
242 | foreach ($where as $clause) {
243 | $modifier = $mapped_wheres === '' ? '' : $clause[0];
244 | $mapped_wheres .= " {$modifier} {$clause[1]} {$clause[2]} ?";
245 | }
246 | return $mapped_wheres;
247 | }
248 |
249 | /**
250 | * This method returns the number of rows in a table.
251 | * @param string $table
252 | * @return int|bool
253 | * @throws Exception
254 | */
255 | public function count($table)
256 | {
257 | $this->sql = "SELECT COUNT(*) FROM {$table}";
258 | try {
259 | $statement = $this->pdo->prepare($this->sql);
260 | $statement->execute();
261 | return $statement->fetchColumn();
262 | } catch (PDOException $e) {
263 | $this->handlePDOException($e);
264 | }
265 | return false;
266 | }
267 |
268 | /**
269 | * This method returns the number of rows in a table where one or more conditions are matched.
270 | * @param string $table
271 | * @param $where
272 | * @param string $columns
273 | * @return int|bool
274 | * @throws Exception
275 | */
276 | public function countWhere($table, $where)
277 | {
278 | $where = $this->prepareWhere($where);
279 | $mapped_wheres = $this->prepareMappedWheres($where);
280 | $where = array_column($where, 3);
281 | $this->sql = "SELECT COUNT(*) FROM {$table} WHERE {$mapped_wheres}";
282 | try {
283 | $statement = $this->pdo->prepare($this->sql);
284 | $statement->execute($where);
285 | return $statement->fetchColumn();
286 | } catch (PDOException $e) {
287 | $this->handlePDOException($e);
288 | }
289 | return false;
290 | }
291 |
292 | /**
293 | * This method deletes rows from a table in a database.
294 | * @param string $table
295 | * @param string $limit
296 | * @return int
297 | * @throws Exception
298 | */
299 | public function delete($table, $limit = "")
300 | {
301 | $limit = $this->prepareLimit($limit);
302 | $this->sql = "DELETE FROM {$table} {$limit}";
303 | try {
304 | $statement = $this->pdo->prepare($this->sql);
305 | $statement->execute();
306 | return $statement->rowCount();
307 | } catch (PDOException $e) {
308 | $this->handlePDOException($e);
309 | }
310 | return 0;
311 | }
312 |
313 | /**
314 | * This method deletes rows from a table in a database where one or more conditions are matched.
315 | * @param string $table
316 | * @param $where
317 | * @param $limit
318 | * @return int
319 | * @throws Exception
320 | */
321 | public function deleteWhere($table, $where, $limit = "")
322 | {
323 | $limit = $this->prepareLimit($limit);
324 | $where = $this->prepareWhere($where);
325 | $mapped_wheres = $this->prepareMappedWheres($where);
326 | $where = array_column($where, 3);
327 | $this->sql = "DELETE FROM {$table} WHERE {$mapped_wheres} {$limit}";
328 | try {
329 | $statement = $this->pdo->prepare($this->sql);
330 | $statement->execute($where);
331 | return $statement->rowCount();
332 | } catch (PDOException $e) {
333 | $this->handlePDOException($e);
334 | }
335 | return 0;
336 | }
337 |
338 | /**
339 | * This method inserts data into a table in a database.
340 | * @param string $table
341 | * @param $parameters
342 | * @return string
343 | * @throws Exception
344 | */
345 | public function insert($table, $parameters)
346 | {
347 | $names = $this->prepareCommaSeparatedColumnNames($parameters);
348 | $values = $this->prepareCommaSeparatedColumnValues($parameters);
349 | $this->sql = sprintf(
350 | 'INSERT INTO %s (%s) VALUES (%s)',
351 | $table,
352 | $names,
353 | $values
354 | );
355 | try {
356 | $statement = $this->pdo->prepare($this->sql);
357 | $statement->execute($parameters);
358 | return $this->pdo->lastInsertId();
359 | } catch (PDOException $e) {
360 | $this->handlePDOException($e);
361 | }
362 | return "";
363 | }
364 |
365 | /**
366 | * This method prepares the comma separated names for the query builder.
367 | * @param $parameters
368 | * @return string
369 | */
370 | private function prepareCommaSeparatedColumnNames($parameters)
371 | {
372 | return implode(', ', array_keys($parameters));
373 | }
374 |
375 | /**
376 | * This method prepares the comma separated values for the query builder.
377 | * @param $parameters
378 | * @return string
379 | */
380 | private function prepareCommaSeparatedColumnValues($parameters)
381 | {
382 | return ':' . implode(', :', array_keys($parameters));
383 | }
384 |
385 | public function iUpDate($table, $parameters, $where = "")
386 | {
387 | $set = $this->prepareNamed($parameters);
388 |
389 | $this->sql = sprintf(
390 | 'UPDATE %s SET %s WHERE %s',
391 | $table,
392 | $set,
393 | $where
394 | );
395 | try {
396 | $statement = $this->pdo->prepare($this->sql);
397 | $statement->execute($parameters);
398 | return true;
399 | } catch (PDOException $e) {
400 | $this->handlePDOException($e);
401 | }
402 | return "";
403 | }
404 |
405 | /**
406 | * This method prepares the named columns.
407 | * @param $parameters
408 | * @return string
409 | */
410 | private function prepareNamed($parameters)
411 | {
412 | return implode(', ', array_map(
413 | static function ($property) {
414 | return "{$property} = :{$property}";
415 | },
416 | array_keys($parameters)
417 | ));
418 | }
419 |
420 | /**
421 | * This method updates data in a table in a database.
422 | * @param string $table
423 | * @param $parameters
424 | * @param string $limit
425 | * @return int
426 | * @throws Exception
427 | */
428 | public function update($table, $parameters, $limit = "")
429 | {
430 | $limit = $this->prepareLimit($limit);
431 | $set = $this->prepareNamed($parameters);
432 | $this->sql = sprintf(
433 | 'UPDATE %s SET %s %s',
434 | $table,
435 | $set,
436 | $limit
437 | );
438 | try {
439 | $statement = $this->pdo->prepare($this->sql);
440 | $statement->execute($parameters);
441 | return $statement->rowCount();
442 | } catch (PDOException $e) {
443 | $this->handlePDOException($e);
444 | }
445 | return 0;
446 | }
447 |
448 | /**
449 | * This method updates data in a table in a database where one or more conditions are matched.
450 | * @param string $table
451 | * @param $parameters
452 | * @param $where
453 | * @param string $limit
454 | * @return int
455 | * @throws Exception
456 | */
457 | public function updateWhere($table, $parameters, $where, $limit = "")
458 | {
459 | $limit = $this->prepareLimit($limit);
460 | $set = $this->prepareUnnamed($parameters);
461 | $parameters = $this->prepareParameters($parameters);
462 | $where = $this->prepareWhere($where);
463 | $mapped_wheres = $this->prepareMappedWheres($where);
464 | $where = array_column($where, 3);
465 | $this->sql = sprintf(
466 | 'UPDATE %s SET %s WHERE %s %s',
467 | $table,
468 | $set,
469 | $mapped_wheres,
470 | $limit
471 | );
472 | try {
473 | $statement = $this->pdo->prepare($this->sql);
474 | $statement->execute(array_merge($parameters, $where));
475 | return $statement->rowCount();
476 | } catch (PDOException $e) {
477 | $this->handlePDOException($e);
478 | }
479 | return 0;
480 | }
481 |
482 | /**
483 | * This method prepares the unnamed columns.
484 | * @param $parameters
485 | * @return string
486 | */
487 | private function prepareUnnamed($parameters)
488 | {
489 | return implode(', ', array_map(
490 | static function ($property) {
491 | return "{$property} = ?";
492 | },
493 | array_keys($parameters)
494 | ));
495 | }
496 |
497 | /**
498 | * This method prepares the parameters with numeric keys.
499 | * @param $parameters
500 | * @param int $counter
501 | * @return mixed
502 | */
503 | private function prepareParameters($parameters, $counter = 1)
504 | {
505 | foreach ($array = $parameters as $key => $value) {
506 | unset($parameters[$key]);
507 | $parameters[$counter] = $value;
508 | $counter++;
509 | }
510 | return $parameters;
511 | }
512 |
513 | /**
514 | * This method selects all of the rows from a table in a database.
515 | * @param string $table
516 | * @return array|int
517 | * @throws Exception
518 | */
519 | public function describe($table)
520 | {
521 | $this->sql = "DESCRIBE {$table}";
522 | try {
523 | $statement = $this->pdo->prepare($this->sql);
524 | $statement->execute();
525 | return $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
526 | } catch (PDOException $e) {
527 | $this->handlePDOException($e);
528 | }
529 | return 0;
530 | }
531 |
532 | /**
533 | * This method executes raw SQL against a database.
534 | * @param string $sql
535 | * @param array $parameters
536 | * @return array|int
537 | * @throws Exception
538 | */
539 | public function raw($sql, array $parameters = [])
540 | {
541 | try {
542 | $this->sql = $sql;
543 | $statement = $this->pdo->prepare($sql);
544 | $statement->execute($parameters);
545 | $output = $statement->rowCount();
546 | if (stripos($sql, "SELECT") === 0) {
547 | $output = $statement->fetchAll(PDO::FETCH_CLASS, $this->class_name ?: "stdClass");
548 | }
549 | return $output;
550 | } catch (PDOException $e) {
551 | $this->handlePDOException($e);
552 | }
553 | return 0;
554 | }
555 |
556 | /**
557 | * This method binds values from an array to the PDOStatement.
558 | * @param PDOStatement $PDOStatement
559 | * @param $array
560 | * @param int $counter
561 | */
562 | private function prepareBindings(PDOStatement $PDOStatement, $array, $counter = 1)
563 | {
564 | foreach ($array as $key => $value) {
565 | $PDOStatement->bindParam($counter, $value);
566 | $counter++;
567 | }
568 | }
569 | }
--------------------------------------------------------------------------------