summaryrefslogtreecommitdiff
path: root/public/app/extends
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
commit059e0d95d0c28bc5060e87e146eaf7411f51bf90 (patch)
tree7c21ac432f16dde2329a0d5954d70711eceb48e6 /public/app/extends
parent26cd8ee99659ef1926c96a049c93645ffc9b169d (diff)
downloadgyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.gz
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.bz2
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.zip
skeleton commit; based on an anom project
Diffstat (limited to 'public/app/extends')
-rw-r--r--public/app/extends/.gitkeep0
-rw-r--r--public/app/extends/Cache_service.php82
-rw-r--r--public/app/extends/Classroom_manager.php106
-rw-r--r--public/app/extends/Classroom_user.php121
-rw-r--r--public/app/extends/Send_mail.php212
5 files changed, 521 insertions, 0 deletions
diff --git a/public/app/extends/.gitkeep b/public/app/extends/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/public/app/extends/.gitkeep
diff --git a/public/app/extends/Cache_service.php b/public/app/extends/Cache_service.php
new file mode 100644
index 0000000..52311f3
--- /dev/null
+++ b/public/app/extends/Cache_service.php
@@ -0,0 +1,82 @@
+<?php
+namespace app\extends;
+
+use app\models\cms\Course_model;
+use app\models\admin\Privilege_model;
+
+/** Cache service
+ * -----------------------------------------------------------------------------
+ *
+ * class that gathers common source-expensive queries
+ * and helps for performance optimization
+ *
+ * when forcing cache creation the following option is used:
+ * $options = PROXY_IGNORE_CACHE
+ *
+ */
+class Cache_service
+{
+
+ public static function courses_struct( $options = 0 )
+ {
+ return proxy( // cache-get courses
+ [\app\models\cms\Course_model::class, 'courses_struct'],
+ [], CACHE_ROOT_TTL,
+ $options
+ );
+ }
+
+ public static function files_attributes( $options = 0 )
+ {
+ return proxy(
+ [\app\models\cms\Course_model::class, 'files'],
+ [], CACHE_ROOT_TTL,
+ $options
+ );
+ }
+
+
+ public static function privileges_hierarchy( $options = 0 )
+ {
+ return proxy(
+ [\app\models\admin\Privilege_model::class, 'privileges'],
+ [], CACHE_ROOT_TTL,
+ $options
+ );
+ }
+
+ public static function pages_list( $options = 0 )
+ {
+ return proxy(
+ [\app\models\cms\Course_model::class, 'pages'],
+ [], CACHE_ROOT_TTL,
+ $options
+ );
+ }
+
+
+ /** entity
+ *
+ * create and retrieve a cached array of some entity
+ *
+ * @param $entiry (string|method): method of the main CMS model
+ * NOTE: CRITICAL: method must exist in the main CMS model
+ */
+ public static function entity( $entity, $options = 0 )
+ {
+ $allowed_methods = [ // to make sure method existence
+ 'page',
+ 'course',
+ 'lesson',
+ ];
+
+ // TODO: needs testing before release
+ // return proxy(
+ // [\app\models\admin\Course_model::class, $entity],
+ // [], CACHE_ROOT_TTL,
+ // $options
+ // );
+ }
+
+
+} \ No newline at end of file
diff --git a/public/app/extends/Classroom_manager.php b/public/app/extends/Classroom_manager.php
new file mode 100644
index 0000000..4d38ba3
--- /dev/null
+++ b/public/app/extends/Classroom_manager.php
@@ -0,0 +1,106 @@
+<?php
+namespace app\extends;
+
+use UserManager;
+use Registry;
+
+
+/** ClassUserManager
+ *
+ * extends (anom's) UserManager
+ *
+ * this way it specializes the UserManagement
+ * according to this app's special rules
+ *
+ */
+class Classroom_manager extends UserManager
+{
+
+ private $index_key = 'email'; // identifing user field (username, email, etc )
+
+ protected $user;
+
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+
+ /** register user
+ *
+ * register into database;
+ * send OTP for account confirmation
+ *
+ * @param (void) : user properties passed via REQUEST->POST
+ *
+ */
+ public function register_user()
+ {
+ $req = Registry::get('REQUEST');
+ $register = User_model::registerUser($req->POST);
+
+ if ($register['success']) {
+
+ //create OTP;
+ $otp = $this->create_OPT($register['id']);
+
+ // send for account confirmation
+
+
+ } else {
+ print_r($register);
+ }
+
+ }
+
+
+ public function login_user()
+ {
+
+ }
+
+
+ /** forgot password
+ *
+ * send an otp
+ * (then verify)
+ *
+ */
+ public function forgot_password()
+ {
+
+ }
+
+
+ /** create OTP
+ *
+ * create a random OTP
+ * keep it into User's database Table
+ *
+ * @param $id (int): user id
+ *
+ */
+ public function create_OTP($id)
+ {
+ $otp = rand(100000,999999);
+ User_model::set_OTP($id, $otp);
+
+ return true;
+ }
+
+
+ /** Verify Account
+ *
+ * check if posted OTP matched the one sent to the user
+ *
+ * @param $identity (array): pair of [index_key => identity_value]
+ * example: [ 'email' => 'geo@roptron.gr' ]
+ *
+ */
+ public function verify_otp($identity, $otp)
+ {
+
+ }
+
+
+} \ No newline at end of file
diff --git a/public/app/extends/Classroom_user.php b/public/app/extends/Classroom_user.php
new file mode 100644
index 0000000..c27c4bf
--- /dev/null
+++ b/public/app/extends/Classroom_user.php
@@ -0,0 +1,121 @@
+<?php
+namespace app\extends;
+
+use User;
+use Registry;
+
+
+/** Classroom_user
+ *
+ * extends (anom's) User
+ *
+ * this way it specializes the UserManagement
+ * according to this app's special rules
+ *
+ * Add privileges concept
+ * ---
+ * A Class_user has privileges (which are deferent than Roles)
+ * In fact, privileges extend the authorization requirements for users
+ *
+ * Authorized content is marked with some 'minimum Privilege'
+ * So the content is available to the READER(s) that have certain Privileges
+ *
+ */
+class Classroom_user extends User
+{
+
+ /** user id
+ * @var int
+ */
+ private $id;
+
+ /** user privileges
+ * @var array
+ */
+ private $privileges = [];
+
+ /** user name
+ * @var string
+ */
+ private $name;
+
+ /** SETTERS AND GETTERS
+ * for id, name, privileges attributes
+ * -------------------------------------------------------------------------
+ */
+
+ // id
+ // --- -- -- - - -
+
+ /** getID
+ * @return int
+ */
+ public function getID(): int
+ {
+ return $this->id;
+ }
+
+ /** setID()
+ *
+ * @param int : user id
+ *
+ * @return User
+ */
+ public function setID(int $id): self
+ {
+ $this->id = $id;
+ return $this;
+ }
+
+
+ // name
+ // --- -- -- - - -
+
+ /** getName
+ * @return int
+ */
+ public function getName(): string
+ {
+ return $this->name;
+ }
+
+ /** setName()
+ *
+ * @param string : user's full name
+ *
+ * @return User
+ */
+ public function setName(string $name): self
+ {
+ $this->name = $name;
+ return $this;
+ }
+
+
+ // privileges
+ // --- -- -- - - -
+
+ /** getPrivileges
+ *
+ * @return privileges (array)
+ */
+ public function getPrivileges(): array
+ {
+ return $this->privileges;
+ }
+
+
+ /** setPrivileges()
+ *
+ * @param array $privileges
+ *
+ * @return User
+ */
+ public function setPrivileges(array $privileges): self
+ {
+ $this->privileges = $privileges;
+ return $this;
+ }
+
+
+} \ No newline at end of file
diff --git a/public/app/extends/Send_mail.php b/public/app/extends/Send_mail.php
new file mode 100644
index 0000000..52eab53
--- /dev/null
+++ b/public/app/extends/Send_mail.php
@@ -0,0 +1,212 @@
+<?php
+namespace app\extends;
+
+use PHPMailer\PHPMailer\PHPMailer;
+use PHPMailer\PHPMailer\SMTP;
+use PHPMailer\PHPMailer\Exception;
+
+
+class Send_mail
+{
+ /** send_activation_code
+ *
+ * construct the envelope = [
+ * email => user email
+ * name => user full name
+ * subject => subject
+ * body => email message body
+ * ]
+ *
+ * then send the email;
+ *
+ * @param $activator (array): [
+ * email => user email,
+ * name => user full name,
+ * code => activation code
+ * ]
+ *
+ * @return success_starus (boolean)
+ *
+ * TODO:
+ * check for email templating:
+ * + https://stackoverflow.com/questions/2391171/how-to-get-output-from-local-script-in-php
+ * + https://stackoverflow.com/questions/171318/how-do-i-capture-php-output-into-a-variable
+ */
+ public static function send_activation_code($activator)
+ {
+ $activation_url = SITE_URL ."/account/activate?ticket=". $activator['code'];
+ $envelope = [
+ 'email' => $activator['email'],
+ 'name' => $activator['name'],
+ 'subject' => 'Εγγραφή στο Classroom',
+ 'body' => "Χαίρετε,<br>
+ το παρόν αυτοποιημένο email σάς έχει σταλεί
+ γιατί έχει γίνει αίτημα εγγραφής σας στο Classroom.<br>
+ <br>
+ Όνομα επαφής: <b>". $activator['name'] ."</b><br>
+ Email : <b>". $activator['email'] ."</b><br>
+ <br>
+ Για να ενεργοποιήσετε την πρόσβασή σας στο site
+ θα πρέπει ακολουθήσετε τον παρακάτω σύνδεσμο:
+ <a href=\"". $activation_url ."\">". $activation_url ."</a>.
+ <br>
+ <br>
+ Μετά την ενεργοποίηση θα έχετε πρόσσβαση
+ στο περιεχόμενο του site.<br>
+ Μην απαντήσετε στο email,
+ δεν υπάρχει φυσική επαφή η οποία να λαμβάνει τυχόν replies."
+ ];
+
+ switch (DEFAULT_MAIL_SERVICE) {
+ case 'mailjet':
+ $reply = self::send_mailjet($envelope);
+ break;
+
+ case 'phpMailer':
+ $reply = self::send_phpMailer($envelope);
+ break;
+ }
+
+ return $reply;
+
+ }
+
+
+ /** send phpMailer
+ *
+ * send email using phpMailer class;
+ * optional SMTP server configuration;
+ *
+ * @param $envelope
+ * @return success_starus (boolean)
+ */
+ public static function send_phpMailer($envelope)
+ {
+ $mail = new PHPMailer();
+
+ # // setup smpt
+ $mail->SMTPDebug = 3;
+ $mail->IsSMTP();
+ $mail->Host = MAIL_HOST;
+ # $mail->SMPTAuth = false;
+ $mail->SMTPSecure = MAIL_ENCRYPTION;
+ # // $mail->Protocol = 'mail';
+
+ $mail->Mailer = MAIL_MAILER;
+ $mail->Port = MAIL_PORT;
+ $mail->Username = MAIL_USERNAME;
+ $mail->Password = MAIL_PASSWORD;
+
+ // setup format
+ $mail->CharSet = 'utf-8';
+ $mail->IsHTML(true);
+
+ // setup THE mail
+ // --- -- -- - - -
+ // It's important not to use the submitter's address as the from address
+ // as it's forgery, which will cause your messages to fail SPF checks.
+ // Use an address in your own domain as the from address;
+ // put the submitter's address in a reply-to
+
+ $mail->setFrom(NO_REPLY_EMAIL, MAIL_FROM_NAME);
+ $mail->addAddress($envelope['email'], $envelope['name']);
+ $mail->addReplyTo(REPLY_TO_EMAIL, MAIL_FROM_NAME);
+ $mail->Subject = $envelope['subject'];
+ $mail->Body = $envelope['body'];
+
+ return (!$mail->send()) ? false : true;
+
+ }
+
+
+ /** send_mailjet
+ *
+ * send email using CURL mail-relay of Mailjet
+ *
+ * @param $envelope
+ * @return success_starus (boolean)
+ */
+ public static function send_mailjet($envelope)
+ {
+ $body = [
+ 'Messages' => [
+ [
+ 'From' => [
+ 'Email' => REPLY_TO_EMAIL,
+ 'Name' => MAIL_FROM_NAME
+ ],
+ 'To' => [
+ [
+ 'Email' => $envelope['email'],
+ 'Name' => $envelope['name']
+ ]
+ ],
+ 'Subject' => $envelope['subject'],
+ 'HTMLPart' => $envelope['body']
+ ]
+ ]
+ ];
+
+ $ch = curl_init();
+
+ curl_setopt($ch, CURLOPT_URL, "https://api.mailjet.com/v3.1/send");
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+ 'Content-Type: application/json')
+ );
+ curl_setopt(
+ $ch,
+ CURLOPT_USERPWD,
+ MAILJET_APIKEY. ':'. MAILJET_SECRET
+ );
+ $server_output = curl_exec($ch);
+ curl_close ($ch);
+
+ $response = json_decode($server_output);
+
+ return ($response->Messages[0]->Status == 'success');
+
+ }
+
+
+
+}
+
+
+
+
+
+/** NOTE:
+ * -----------------------------------------------------------------------------
+ * (brainstorming)
+ *
+
+Optimization per concept alternative technologies
+--- -- -- - - -
+
+## Session
+File Session
+Database Session
+Redis Session
+Memcached Session
+
+---
+
+## Cache
+File Cache
+Database Cache
+Redis Cache
+Memcashed Cache
+
+---
+
+## Log
+File Log
+Database Log
+Log event to Slack
+Log event to Email
+
+ * -----------------------------------------------------------------------------
+ */