From 40313e97d1d6b300297e4678241615ce9c60734b Mon Sep 17 00:00:00 2001 From: Edward Teach Date: Wed, 12 Dec 2018 20:49:30 -0800 Subject: [PATCH] removed major dappurware to separate repos --- CHANGELOG.md | 12 +- app/src/Dappurware/Deployment.php | 447 -------------------------- app/src/Dappurware/Email.php | 492 ----------------------------- app/src/Dappurware/Oauth2Utils.php | 403 ----------------------- app/src/Dappurware/VideoParser.php | 100 ------ composer.json | 2 +- 6 files changed, 12 insertions(+), 1444 deletions(-) delete mode 100644 app/src/Dappurware/Deployment.php delete mode 100644 app/src/Dappurware/Email.php delete mode 100644 app/src/Dappurware/Oauth2Utils.php delete mode 100644 app/src/Dappurware/VideoParser.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b72e48f..dfec17e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ # Changelog ## [Unreleased] +### No Changes + +## [4.0.0] +### Notes +- I promise this will be the last major version upgrade for a while (I hope). This was necessary in order to separate out the heavier dappurwares into their own repository so that they could be developed further. The dapurwares that were moved into their own repositories are: + - Deployment + - Email + - Oauth2 + - Video ## [3.0.0] ### Notes @@ -58,7 +67,8 @@ I am bumping the version number on on Dappurware for this release to v3.0.0. Th - Separated Dappurware from the framework. -[Unreleased]: https://github.com/dappur/dappurware/compare/v3.0.0...HEAD +[Unreleased]: https://github.com/dappur/dappurware/compare/v4.0.0...HEAD +[4.0.0]: https://github.com/dappur/dappurware/compare/v3.0.0...v4.0.0 [3.0.0]: https://github.com/dappur/dappurware/compare/v1.1.2...v3.0.0 [1.1.2]: https://github.com/dappur/dappurware/compare/v1.1.1...v1.1.2 [1.1.1]: https://github.com/dappur/dappurware/compare/v1.1.0...v1.1.1 diff --git a/app/src/Dappurware/Deployment.php b/app/src/Dappurware/Deployment.php deleted file mode 100644 index de6356c..0000000 --- a/app/src/Dappurware/Deployment.php +++ /dev/null @@ -1,447 +0,0 @@ -documentRoot = $_SERVER['DOCUMENT_ROOT']; - if (!is_null($documentRoot)) { - $this->documentRoot = $documentRoot; - } - $this->userHome = $_SERVER['HOME']; - if (!is_null($userHome)) { - $this->userHome = $userHome; - } - $this->gitBinPath = $gitBinPath; - $this->logFile = dirname($this->documentRoot) . '/storage/log/deployment/deployment-'.date("YmdGis").'.log'; - $this->repoDir = dirname($this->documentRoot) . '/repo'; - $this->certFolder = dirname($this->documentRoot) . '/storage/certs/deployment'; - $this->certFileName = $certFileName; - $this->certPath = realpath($this->certFolder . "/" . $this->certFileName); - set_time_limit($timeLimit); - - // Check for settings.json - $settingsFile = json_decode(file_get_contents(realpath($documentRoot) . "/../settings.json")); - if (!$settingsFile) { - die($this->logEntry("Could not locate a settings.json file in the document root.")); - } - $this->settings = $settingsFile; - - // Check Repo Url - if (!isset($settingsFile->deployment->repo_url) || $settingsFile->deployment->repo_url == "") { - die($this->logEntry("Please ensure that you have a repo URL in the deployment section of settings.json.")); - } - $this->repoUrl = $settingsFile->deployment->repo_url; - - // Check Repo Branch - if (!isset($settingsFile->deployment->repo_branch) || $settingsFile->deployment->repo_branch == "") { - die($this->logEntry("Please ensure that you have a repo branch in the deployment section of settings.json.")); - } - $this->repoBranch = $settingsFile->deployment->repo_branch; - - // Check Project Name - if (!isset($settingsFile->framework) || $settingsFile->framework == "") { - die($this->logEntry("Please ensure that you have a valid framework name in settings.json.")); - } - } - - // Initialize Dappur - public function initDappur() - { - echo $this->logEntry("Initializing Dappur Framework..."); - $this->validateDocumentRoot(); - $this->installUpdateComposer(); - $this->checkGit(); - $this->checkInstallRepo(); - $this->checkPhinx(); - $this->migrateUp(); - echo $this->logEntry("Framework initialization complete."); - } - - // Execute - public function execute() - { - echo $this->logEntry("Checking requirements..."); - $this->validateDocumentRoot(); - $this->installUpdateComposer(); - $this->checkGit(); - $this->checkInstallRepo(); - $this->checkConnection(); - echo $this->logEntry("Requirements validated!"); - } - - public function migrate() - { - echo $this->logEntry("Beginning Migration..."); - $this->checkPhinx(); - $this->migrateUp(); - } - - // Make sure PHP user has all appropriate permissions and that server - // structure is correct - private function validateDocumentRoot() - { - - // Check that the DOCUMENT_ROOT is a directory called `public`. - if (end(explode('\\', $this->documentRoot)) != "public" && - end(explode('/', $this->documentRoot)) != "public") { - die($this->logEntry("Your servers DOCUMENT_ROOT needs to be a directory called `public`")); - } - - // Check that the DOCUMENT_ROOT parent directory is writable. - if (!is_writable(dirname($this->documentRoot))) { - die( - $this->logEntry( - "Web server user does not have access to the DOCUMENT_ROOT's parent directory. ". - "This is required in order for Dappur to function properly." - ) - ); - } - } - - // Check if composer is installed or download phar and use that. - private function installUpdateComposer() - { - if (!is_file(dirname($this->documentRoot) . '/composer.phar')) { - // Download composer to the DOCUMENT_ROOT's parent directory. - if (file_put_contents( - dirname($this->documentRoot) . '/composer.phar', - fopen("https://getcomposer.org/download/1.7.1/composer.phar", 'r') - )) { - echo $this->logEntry("Composer downloaded successfully. Making composer.phar executable..."); - // CD into DOCUMENT_ROOT parent and make composer.phar executable - exec("cd " . dirname($this->documentRoot) . " && chmod +x composer.phar"); - } else { - echo $this->logEntry("Could not get Composer working. Please check your settings and try again."); - } - } else { - // Check that composer is working - $check_composer = shell_exec(dirname($this->documentRoot) . "/composer.phar" . ' --version 2>&1'); - echo $this->logEntry($check_composer); - if (strpos($check_composer, 'omposer version')) { - // Check for Composer updates - $update_composer = shell_exec(dirname($this->documentRoot) . "/composer.phar self-update 2>&1"); - echo $this->logEntry("Checking For Composer Update..."); - echo $this->logEntry($update_composer); - } - } - } - - private function checkGit() - { - // Check that git is installed - $check_git = shell_exec($this->gitBinPath . " --version"); - echo $this->logEntry($check_git); - if (!strpos($check_git, 'it version ')) { - die($this->logEntry("
Git is required in order for auto deployment to work. ".
-                "Please check the deploy.init.log for errors.
")); - } - } - - private function checkInstallRepo() - { - - // Create repository directory if it doesnt exist. - if (!is_dir($this->repoDir)) { - echo $this->logEntry("Repository directory does not exist. Creating it now..."); - if (!mkdir($this->repoDir)) { - die($this->logEntry("There was an error creating the repository directory. ". - "Please check your PHP user's permissions and try again.")); - } else { - echo $this->logEntry("Repository directory created successfully."); - - // Initialize and prepare the git repository - $this->initializeRepository(); - - // Update composer - $this->updateComposer(); - } - } else { - if (!is_file($this->repoDir . '/config')) { - echo $this->logEntry("Repository doesn't exist. Attempting to create now..."); - // Initialize and prepare the git repository - $this->initializeRepository(); - - // Update the repository - $this->updateRepository(); - - // Update composer - $this->updateComposer(); - } else { - // Update the repository. - $this->updateRepository(); - - // Update composer - $this->updateComposer(); - } - } - } - - private function initializeRepository() - { - - // Check Known Hosts file for github.com and add using ssh-keyscan - if (is_file('~/.ssh/known_hosts')) { - $known_hosts = file_get_contents('~/.ssh/known_hosts'); - if (!strpos($known_hosts, "github.com")) { - // Add Github to the known hosts - $add_github = shell_exec('ssh-keyscan github.com >> ~/.ssh/known_hosts'); - $this->logEntry($add_github); - } - } else { - // Add Github to the known hosts - $add_github = shell_exec('ssh-keyscan github.com >> ~/.ssh/known_hosts'); - } - - // Create the Mirror Repository - $create_repo_mirror = shell_exec( - 'cd ' . $this->repoDir . ' && ' . 'GIT_SSH_COMMAND="ssh -i ' . $this->certPath . ' -F /dev/null" ' . $this->gitBinPath . ' clone --mirror ' . $this->repoUrl . " . 2>&1" - ); - echo $this->logEntry($create_repo_mirror); - if (strpos($create_repo_mirror, 'ermission denied (publickey)') || - strpos($create_repo_mirror, 'Repository not found.') || - strpos($create_repo_mirror, 'and the repository exists')) { - echo $this->logEntry("Access denied to repository... Creating deployment key now..."); - die($this->logEntry( - "Please add the following public key between the dashes to your ". - "deployment keys for the repository and run this script again.\n" . - str_repeat("-", 80) . "\n" . - $this->getDeployKey() . "\n" . - str_repeat("-", 80) - )); - } - - // Do the initial checkout - $git_checkout = shell_exec( - 'cd ' . $this->repoDir . - ' && GIT_WORK_TREE=' . dirname($this->documentRoot) . ' ' . - $this->gitBinPath . ' checkout ' . $this->repoBranch . ' -f 2>&1' - ); - echo $this->logEntry($git_checkout); - // Get the deployment commit hash - $commit_hash = exec('cd ' . $this->repoDir . ' && ' . $this->gitBinPath . ' rev-parse --short HEAD 2>&1'); - echo $this->logEntry("Deployed Commit: " . $commit_hash); - } - - private function updateRepository() - { - - // Fetch any new changes - $git_fetch = exec('cd ' . $this->repoDir . ' && ' . 'GIT_SSH_COMMAND="ssh -i ' . $this->certPath . ' -F /dev/null" ' . $this->gitBinPath . ' fetch 2>&1'); - if (empty($git_fetch)) { - echo $this->logEntry("There is nothing new to fetch from this repository."); - } else { - echo $this->logEntry($git_fetch); - // Do the checkout - shell_exec( - 'cd ' . $this->repoDir . ' && GIT_WORK_TREE=' . dirname($this->documentRoot) . ' ' . - $this->gitBinPath . ' checkout ' . $this->repoBranch . ' -f 2>&1' - ); - // Get the deployment commit hash - $commit_hash = exec( - 'cd ' . $this->repoDir . ' && ' . $this->gitBinPath . ' rev-parse --short HEAD 2>&1' - ); - echo $this->logEntry("Deployed Commit: " . $commit_hash); - } - } - - private function updateComposer() - { - $checkLockFile = exec('cd ' . $this->repoDir . ' && ' . $this->gitBinPath . ' ls-files --error-unmatch composer.lock 2>&1'); - if (strpos($checkLockFile, 'did not match any file')) { - $update_composer = shell_exec( - 'cd ' . dirname($this->documentRoot) . ' && ' . - dirname($this->documentRoot) . '/composer.phar update --no-dev 2>&1' - ); - } else { - $update_composer = shell_exec( - 'cd ' . dirname($this->documentRoot) . ' && ' . - dirname($this->documentRoot) . '/composer.phar install --no-dev 2>&1' - ); - } - - echo $this->logEntry($update_composer); - if (!strpos($update_composer, 'Generating autoload files')) { - echo $this->logEntry("An error might have occured while updating composer. ". - "Please check the deployment log to confirm."); - } else { - echo $this->logEntry("Composer updated successfully!"); - } - } - - private function getDeployKey() - { - // Create certificate folder if it does not exist - if (!is_dir($this->certFolder)) { - mkdir($this->certFolder, 0755, true); - } - - if (file_exists($this->certFolder . '/' . $this->certFileName) && - file_exists($this->certFolder . '/' . $this->certFileName . ".pub")) { - return file_get_contents($this->certFolder . '/' . $this->certFileName . ".pub"); - } elseif (!file_exists($this->certFolder . '/' . $this->certFileName)) { - // Create the deploy key with ssh-keygen - $generate_key = exec( - "ssh-keygen -q -N '' -t rsa -b 4096 -f " . $this->certFolder . "/" . $this->certFileName - ); - echo $this->logEntry($generate_key); - // Get the contents of the public key - $public_key = file_get_contents($this->certFolder . '/' . $this->certFileName . '.pub'); - // Install the deploy key if not already done - - // Return the public key - return $public_key; - } else { - // Install the deploy key if not already done - - // Return the public key - return file_get_contents($this->certFolder . '/' . $this->certFileName . '.pub'); - } - } - - private function checkConnection() - { - $output == array(); - - $database = $this->settings->db->{$this->settings->environment}; - - $output['check_file'] = false; - $output['check_construct'] = false; - - - // Check the mysql database in the settings file. - if ($database->host != "" - && $database->database != "" - && $database->username != "" - && $database->password != "") { - if (!@mysqli_connect( - $database->host, - $database->username, - $database->password, - $database->database - )) { - echo $this->logEntry("MySQL Connection Error: " . mysqli_connect_error()); - } else { - echo $this->logEntry("Successfully connected to " . $database->database . "."); - $output['check_file'] = true; - } - } - - if ($output['check_file'] == false && $output['check_construct'] == false) { - die($this->logEntry("Could not successfully connect to a database. ". - "Please check your settings and run this script again.")); - } else { - return $output; - } - } - - private function checkPhinx() - { - - // Check if Phinx is installed - if (!is_file(dirname($this->documentRoot) . '/vendor/robmorgan/phinx/bin/phinx')) { - // Install/Update Phinx globally in composer - $install_phinx = shell_exec( - "cd " . dirname($this->documentRoot) . " && ./composer.phar require robmorgan/phinx 2>&1" - ); - echo $this->logEntry($install_phinx); - if (!strpos($install_phinx, 'for robmorgan/phinx')) { - die($this->logEntry("Phinx is required as a global composer dependency. ". - "Please check that it is installed and your web user has access to it.")); - } - } - - // Check that phinx was installed properly - $check_phinx = shell_exec(dirname($this->documentRoot) . "/vendor/robmorgan/phinx/bin/phinx --version"); - echo $this->logEntry($check_phinx); - if (!strpos($check_phinx, 'https://phinx.org')) { - die($this->logEntry("Phinx is required in order for database migration to work. ". - "Please check the deploy.init.log for errors.")); - } - - // Check for Phinx config file - if (!is_file(dirname($this->documentRoot) . '/phinx.php')) { - die($this->logEntry("You do not appear to have a valid phinx.php file in your project root directory.")); - } else { - echo $this->logEntry("Phinx config: phinx.php found."); - } - } - - private function migrateUp() - { - $migrate_up = shell_exec( - "cd " . dirname($this->documentRoot) . " && ./vendor/robmorgan/phinx/bin/phinx migrate 2>&1" - ); - echo $this->logEntry($migrate_up); - if (!strpos($migrate_up, 'All Done.')) { - echo $this->logEntry("There might have been an error in the database migration. ". - "Please check the logs to be sure."); - } else { - echo $this->logEntry("Database migration completed successfully."); - } - } - - private function logEntry($log_text, $return = true) - { - - // Create log folder if it does not exist - if (!is_dir(dirname($this->logFile))) { - mkdir(dirname($this->logFile), 0755, true); - } - - // Create Log File if it does not exist - if (!file_exists($this->logFile)) { - touch($this->logFile); - } - - // Add Log Entry to file - file_put_contents($this->logFile, date('m/d/Y h:i:s a') . " " . $log_text . "\n", FILE_APPEND); - - if ($return == true) { - return "
" . date('m/d/Y h:i:s a') . " $log_text" . "
"; - } - } -} diff --git a/app/src/Dappurware/Email.php b/app/src/Dappurware/Email.php deleted file mode 100644 index 9d1637c..0000000 --- a/app/src/Dappurware/Email.php +++ /dev/null @@ -1,492 +0,0 @@ -validateTemplate(); - - if ($this->validator->isValid()) { - $requestParams = $this->container->request->getParams(); - - $addTemplate = new EmailsTemplates; - $addTemplate->name = $requestParams['name']; - $addTemplate->slug = $requestParams['slug']; - $addTemplate->description = $requestParams['description']; - $addTemplate->subject = $requestParams['subject']; - $addTemplate->html = $requestParams['html']; - $addTemplate->plain_text = $requestParams['plain_text']; - if ($requestParams['placeholders']) { - $addTemplate->placeholders = json_encode($requestParams['placeholders']); - } - - if ($addTemplate->save()) { - return true; - } - } - - return false; - } - - public function updateTemplate($templateId = null) - { - $template = EmailsTemplates::find($templateId); - - $this->validateTemplate($templateId); - - if ($this->validator->isValid()) { - $requestParams = $this->container->request->getParams(); - - $template->name = $requestParams['name']; - $template->slug = $requestParams['slug']; - $template->description = $requestParams['description']; - $template->subject = $requestParams['subject']; - $template->html = $requestParams['html']; - $template->plain_text = $requestParams['plain_text']; - if ($requestParams['placeholders']) { - $template->placeholders = json_encode($requestParams['placeholders']); - } - - if ($template->save()) { - return true; - } - } - - return false; - } - - private function validateTemplate($templateId = null) - { - $requestParams = $this->container->request->getParams(); - - $this->container->validator->validate( - $this->container->request, - array( - 'name' => array( - 'rules' => V::alnum('\'-".,?!@#$%^&')->length(4, 32), - 'messages' => array( - 'alnum' => 'May only contain letters and numbers.', - 'length' => 'Must be between 4 and 32 characters.' - ) - ), - 'slug' => array( - 'rules' => V::slug()->length(4, 32), - 'messages' => array( - 'slug' => 'Must be in slug format.', - 'length' => 'Must be between 4 and 32 characters.' - ) - ), - 'description' => array( - 'rules' => V::alnum('\'-".,?!@#$%^&')->length(4, 255), - 'messages' => array( - 'alnum' => 'May only contain letters and numbers.', - 'length' => 'Must be between 4 and 255 characters.' - ) - ) - ) - ); - - // Check for Dupe Slug - $slugCheck = EmailsTemplates::where('slug', $requestParams['slug']); - if ($templateId) { - $slugCheck = $slugCheck->where('id', '!=', $templateId); - } - if ($slugCheck->first()) { - $this->container->validator->addError('slug', 'Slug is already in use.'); - } - - // Check Plain Text for HTML - if (strip_tags($requestParams['plain_text']) != $requestParams['plain_text']) { - $this->container->validator->addError('plain_text', 'Plain Text cannot contain HTML.'); - } - - // Process Placeholders - if ($requestParams['placeholders']) { - foreach ($requestParams['placeholders'] as $plvalue) { - if (!V::alnum('_')->lowercase()->validate($plvalue)) { - $this->container->validator->addError( - 'placeholders', - 'All custom data fields must be in this_format.' - ); - } - } - } - - return true; - } - - public function getPlaceholders() - { - $output = array(); - - $users = Users::first()->toArray(); - - foreach (array_keys($users) as $key) { - if ($key == "password" || $key == "permissions") { - continue; - } - - $output['User Info'][] = array("name" => "user_" . str_replace("-", "_", $key)); - } - - // Only include Site Settings - $siteSettings = ConfigGroups::with('config')->find(1); - - foreach ($siteSettings->config as $value2) { - $output[$siteSettings->name][] = array( - "name" => "settings_" . str_replace("-", "_", $value2->name), - "value" => $value2->value - ); - } - - return $output; - } - - public function sendTemplate(array $sendTo, $templateSlug = null, array $params = null) - { - $output = array(); - - $placeholders = $this->getPlaceholders(); - - $template = EmailsTemplates::where('slug', '=', $templateSlug)->first(); - - $recipients = $this->parseRecipients($sendTo); - - if (!empty($recipients)) { - if ($template) { - // Get Email Bodies - $html = $template->html; - $plainText = $template->plain_text; - $subject = $template->subject; - - // Process Custom Placeholders - $tplaceholders = json_decode($template->placeholders); - - if ($tplaceholders) { - foreach ($tplaceholders as $value) { - if (isset($params[$value])) { - $placeholders['Template'][] = array("name" => $value, "value" => $params[$value]); - } - } - } - - // Send Email To Users - if (!empty($recipients['users'])) { - foreach ($recipients['users'] as $uvalue) { - // Process Bodies with Custom and System Placeholders - $userTemp = Users::find($uvalue); - $placeholdersTemp = $placeholders; - - if ($userTemp) { - foreach ($placeholders['User Info'] as $key => $sssvalue) { - $tempValue = str_replace("user_", "", $sssvalue['name']); - $placeholdersTemp['User Info'][$key]['value'] = $userTemp[$tempValue]; - } - } - - $placeholdersTemp = $this->preparePlaceholders($placeholdersTemp); - - // Process HTML Email - $htmlTemp = new \Twig_Environment(new \Twig_Loader_Array([$template->slug . '_html' => $html])); - $htmlTemp = $htmlTemp->render($template->slug . '_html', $placeholdersTemp); - - // Process Plain Text Email - $plainTextTemp = new \Twig_Environment( - new \Twig_Loader_Array( - [$template->slug . '_pt' => $plainText] - ) - ); - $plainTextTemp = $plainTextTemp->render($template->slug . '_pt', $placeholdersTemp); - - //Process Subject - $subjectTemp = new \Twig_Environment( - new \Twig_Loader_Array( - [$template->slug . '_sub' => $subject] - ) - ); - $subjectTemp = $subjectTemp->render($template->slug . '_sub', $placeholdersTemp); - - $send = $this->send( - $userTemp->email, - html_entity_decode($subjectTemp), - $htmlTemp, - $plainTextTemp, - $template->id - ); - - if ($send['result']) { - $output['results']['success'][] = array("email" => $userTemp->email); - } - - if (!$send['result']) { - $output['results']['errors'][] = array( - "email" => $userTemp->email, - "error" => $result['error'] - ); - } - } - } - - // Send Email to Email Addresses - if (!empty($recipients['email'])) { - $placeholdersTemp = $this->preparePlaceholders($placeholders); - - foreach ($recipients['email'] as $evalue) { - // Process HTML Email - $htmlTemp = new \Twig_Environment(new \Twig_Loader_Array([$template->slug . '_html' => $html])); - $htmlTemp = $htmlTemp->render($template->slug . '_html', $placeholdersTemp); - - // Process Plain Text Email - $plainTextTemp = new \Twig_Environment(new \Twig_Loader_Array( - [$template->slug . '_pt' => $plainText] - )); - $plainTextTemp = $plainTextTemp->render($template->slug . '_pt', $placeholdersTemp); - - //Process Subject - $subjectTemp = new \Twig_Environment(new \Twig_Loader_Array( - [$template->slug . '_sub' => $subject] - )); - $subjectTemp = $subjectTemp->render($template->slug . '_sub', $placeholdersTemp); - - $send = $this->send( - $evalue, - html_entity_decode($subjectTemp), - $htmlTemp, - $plainTextTemp, - $template->id - ); - - if ($send['result']) { - $output['results']['success'][] = array("email" => $evalue); - } - - if (!$send['result']) { - $output['results']['errors'][] = array("email" => $evalue, "error" => $result['error']); - } - } - } - } - - if (!$template) { - $output['status'] = "error"; - $output['message'] = "No valid template was selected."; - } - } - - if (empty($recipients)) { - $output['status'] = "error"; - $output['message'] = "There were no recipients to send to."; - } - - return $output; - } - - public function sendEmail(array $sendTo, $subject, $html, $plainText) - { - $output = array(); - - $placeholders = Email::getPlaceholders(); - - $recipients = Email::parseRecipients($sendTo); - - if (!empty($recipients)) { - // Send Email To Users - if (!empty($recipients['users'])) { - foreach ($recipients['users'] as $uvalue) { - // Process Bodies with Custom and System Placeholders - $userTemp = Users::find($uvalue); - $placeholdersTemp = $placeholders; - - if ($userTemp) { - foreach ($placeholders['User Info'] as $key => $sssvalue) { - $placeholdersTemp['User Info'][$key]['value'] = - $userTemp[str_replace("user_", "", $sssvalue['name'])]; - } - } - - $placeholdersTemp = $this->preparePlaceholders($placeholdersTemp); - - // Process HTML Email - $htmlTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_html' => $html])); - $htmlTemp = $htmlTemp->render('email_html', $placeholdersTemp); - - // Process Plain Text Email - $plainTextTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_pt' => $plainText])); - $plainTextTemp = $plainTextTemp->render('email_pt', $placeholdersTemp); - - //Process Subject - $subjectTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_sub' => $subject])); - $subjectTemp = $subjectTemp->render('email_sub', $placeholdersTemp); - - $sendEmail = Email::send($userTemp->email, html_entity_decode($subjectTemp), $htmlTemp, $plainTextTemp); - - if ($sendEmail) { - $output['results']['success'][] = array("email" => $userTemp->email); - } - - if (!$sendEmail) { - $output['results']['errors'][] = array("email" => $userTemp->email, "error" => $result['error']); - } - } - } - - // Send Email to Email Addresses - if (!empty($recipients['email'])) { - foreach ($recipients['email'] as $evalue) { - $placeholdersTemp = $this->preparePlaceholders($placeholders); - - // Process HTML Email - $htmlTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_html' => $html])); - $htmlTemp = $htmlTemp->render('email_html', $placeholdersTemp); - - // Process Plain Text Email - $plainTextTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_pt' => $plainText])); - $plainTextTemp = $plainTextTemp->render('email_pt', $placeholdersTemp); - - //Process Subject - $subjectTemp = new \Twig_Environment(new \Twig_Loader_Array(['email_sub' => $subject])); - $subjectTemp = $subjectTemp->render('email_sub', $placeholdersTemp); - - $sendEmail = Email::send($evalue, $subjectTemp, $htmlTemp, $plainTextTemp); - if ($sendEmail) { - $output['results']['success'][] = array("email" => $evalue); - } - if (!$sendEmail) { - $output['results']['errors'][] = array("email" => $evalue, "error" => $result['error']); - } - } - } - } - - if (empty($recipients)) { - $output['status'] = "error"; - $output['message'] = "There were no recipients to send to."; - } - - return $output; - } - - private function parseRecipients(array $sendTo) - { - $output = array(); - $output['users'] = array(); - foreach ($sendTo as $value) { - // If int, get user email - if ($this->auth->findById($value)) { - $output['users'][] = $value; - } - - if (filter_var($value, FILTER_VALIDATE_EMAIL)) { - // If email, check if user - $emailUser = $this->container->auth->findByCredentials(['login' => $value]); - if ($emailUser) { - if (!in_array($value, $output['users'])) { - $output['users'][] = $emailUser->id; - } - } - - if (!$emailUser) { - $output['email'][] = $value; - } - } - - //if other, check to see if user role exists for slug - $role = $this->auth->findRoleBySlug($value); - if ($role) { - $users = $role->users()->get(['id']); - foreach ($users as $value) { - if (!in_array($value, $output['users'])) { - $output['users'][] = $value['id']; - } - } - } - } - - return $output; - } - - private function preparePlaceholders($placeholders) - { - $output = array(); - - foreach ($placeholders as $value) { - foreach ($value as $value2) { - if (isset($value2['value'])) { - $output[$value2['name']] = $value2['value']; - } - } - } - - return $output; - } - - private function send($email, $subject, $html, $plainText, $templateId = null) - { - $output = array(); - - $mail = $this->mail; - $mail->ClearAllRecipients(); - $mail->setFrom($this->config['from-email']); - $mail->addAddress($email); - $mail->addReplyTo($this->config['from-email']); - $mail->isHTML(true); - - $mail->Subject = $subject; - $mail->Body = $html; - $mail->AltBody = $plainText; - - if (!$mail->send()) { - $output['result'] = false; - $output['error'] = $mail->ErrorInfo; - return $output; - } - - if ($this->settings['mail']['logging']) { - //Delete Old Emails - if ($this->settings['mail']['log_retention']) { - Emails::where( - 'created_at', - '<', - Carbon::now()->subDays($this->settings['mail']['log_retention']) - )->delete(); - } - - $addEmail = new Emails; - $addEmail->secure_id = Uuid::uuid4()->toString(); - $addEmail->template_id = $templateId; - $addEmail->send_to = $email; - $addEmail->subject = $subject; - if (in_array("html", $this->settings['mail']['log_details'])) { - $addEmail->html = $html; - } - if (in_array("plain_text", $this->settings['mail']['log_details'])) { - $addEmail->plain_text = $plainText; - } - $addEmail->save(); - } - - $output['result'] = true; - - return $output; - } -} diff --git a/app/src/Dappurware/Oauth2Utils.php b/app/src/Dappurware/Oauth2Utils.php deleted file mode 100644 index 55bf464..0000000 --- a/app/src/Dappurware/Oauth2Utils.php +++ /dev/null @@ -1,403 +0,0 @@ - $value) { - $output[] = "$key=" . rawurlencode($value); - } - - return "POST&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $output)); - } - - public function getCompositeKey($consumerSecret, $requestToken) - { - return rawurlencode($consumerSecret) . '&' . rawurlencode($requestToken); - } - - public function buildAuthorizationHeader($oauth) - { - $output = 'Authorization: OAuth '; - $values = array(); - foreach ($oauth as $key => $value) { - $values[] = "$key=\"" . rawurlencode($value) . "\""; - } - - $output .= implode(', ', $values); - return $output; - } - - public function sendRequest($oauth, $baseURI) - { - $header = array( $this->buildAuthorizationHeader($oauth), 'Expect:'); - - $options = array(CURLOPT_HTTPHEADER => $header, - CURLOPT_HEADER => false, - CURLOPT_URL => $baseURI, - CURLOPT_POST => true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_SSL_VERIFYPEER => false); - - $channel = curl_init(); - curl_setopt_array($channel, $options); - $response = curl_exec($channel); - curl_close($channel); - - return $response; - } - - public function getUserInfo($token, $provider) - { - switch ($provider->slug) { - // Twitter - case 'twitter': - $userInfo = $this->getTwitterUser($token, $provider->resource_url); - break; - // Google - case 'google': - $userInfo = $this->getGoogleUser($token, $provider->resource_url); - break; - // Facebook - case 'facebook': - $userInfo = $this->getFacebookUser($token, $provider->resource_url); - break; - // LinkedIn - case 'linkedin': - $userInfo = $this->getLinkedinUser($token, $provider->resource_url); - break; - // Github - case 'github': - $userInfo = $this->getGithubUser($token, $provider->resource_url); - break; - // Instagram - case 'instagram': - $userInfo = $this->getInstagramUser($token); - break; - // Oauth2 Default - default: - $userInfo = $this->getDefaultUser($token, $provider->resource_url); - break; - } - - $userInfo['access_token'] = $token->access_token; - if (isset($token->token_secret)) { - $userInfo['token_secret'] = $token->token_secret; - } - if (isset($token->refresh_token)) { - $userInfo['refresh_token'] = $token->refresh_token; - } - $userInfo['expires_in'] = $token->expires_in; - - return $userInfo; - } - - private function getInstagramUser($token) - { - - $userInfo['uid'] = $token->user->id; - $fullName = explode(' ', $token->user->full_name, 2); - $userInfo['first_name'] = $fullName[0]; - $userInfo['last_name'] = $fullName[1]; - - return $userInfo; - } - - private function getTwitterUser($token, $resourceUrl) - { - $connection = new TwitterOAuth( - $this->settings['oauth2']['twitter']['client_id'], - $this->settings['oauth2']['twitter']['client_secret'], - $token->access_token, - $token->token_secret - ); - $returnedInfo = $connection->get($resourceUrl, array("include_email" => "true")); - - $userInfo['uid'] = $token->uid; - $fullName = explode(' ', $returnedInfo->name, 2); - $userInfo['first_name'] = $fullName[0]; - $userInfo['last_name'] = $fullName[1]; - - if (isset($returnedInfo->email) && $returnedInfo->email != "") { - $userInfo['email'] = $returnedInfo->email; - } - - return $userInfo; - } - - private function getGoogleUser($token, $resourceUrl) - { - $returnedInfo = $this->apiRequest( - $resourceUrl, - false, - array('Authorization: Bearer '. $token->access_token), - true - ); - $userInfo['uid'] = $returnedInfo->id; - $userInfo['first_name'] = $returnedInfo->given_name; - $userInfo['last_name'] = $returnedInfo->family_name; - - if (isset($returnedInfo->email) && $returnedInfo->email != "") { - $userInfo['email'] = $returnedInfo->email; - } - return $userInfo; - } - - private function getFacebookUser($token, $resourceUrl) - { - $returnedInfo = $this->apiRequest( - $resourceUrl, - false, - array('Authorization: Bearer '. $token->access_token), - true - ); - - $userInfo['uid'] = $returnedInfo->id; - $userInfo['first_name'] = $returnedInfo->first_name; - $userInfo['last_name'] = $returnedInfo->last_name; - - if (isset($returnedInfo->email) && $returnedInfo->email != "") { - $userInfo['email'] = $returnedInfo->email; - } - return $userInfo; - } - - private function getLinkedinUser($token, $resourceUrl) - { - $returnedInfo = $this->apiRequest( - $resourceUrl, - false, - array('Authorization: Bearer '. $token->access_token), - true - ); - - $userInfo['uid'] = $returnedInfo->id; - $userInfo['first_name'] = $returnedInfo->firstName; - $userInfo['last_name'] = $returnedInfo->lastName; - - if (isset($returnedInfo->emailAddress) && $returnedInfo->emailAddress != "") { - $userInfo['email'] = $returnedInfo->emailAddress; - } - - - return $userInfo; - } - - private function getGithubUser($token, $resourceUrl) - { - $returnedInfo = $this->apiRequest( - $resourceUrl, - false, - array('Authorization: Bearer '. $token->access_token, - 'User-Agent:' . $this->container->request->getHeader('HTTP_USER_AGENT')), - true - ); - - $userInfo['uid'] = $returnedInfo->id; - $fullName = explode(' ', $returnedInfo->name, 2); - $userInfo['first_name'] = $fullName[0]; - $userInfo['last_name'] = $fullName[1]; - - if (isset($returnedInfo->email) && $returnedInfo->email != "") { - $userInfo['email'] = $returnedInfo->email; - } - return $userInfo; - } - - private function getDefaultUser($token, $resourceUrl) - { - $returnedInfo = $this->apiRequest( - $resourceUrl, - false, - array('Authorization: Bearer '. $token->access_token), - true - ); - if (isset($returnedInfo->id) && $returnedInfo->id != "") { - $userInfo['uid'] = $returnedInfo->id; - } - return $userInfo; - } - - public function getAccessToken($provider) - { - // Get Access Token - switch ($provider->slug) { - case 'twitter': - $token = $this->getTwitterAccessToken($provider); - break; - case 'facebook': - $token = $this->getFacebookAccessToken($provider); - break; - case 'github': - $token = $this->getGithubAccessToken($provider); - break; - default: - // Verify the state matches our stored state - $oauth2State = $this->container->session->get('oauth2-state'); - $returnedState = $this->container->request->getParam('state'); - - if ((string) $oauth2State !== (string) $returnedState) { - (object) $token['error'] = (object) array( - "message" => (string) "Oauth2 Error: Session state did not match. Please try again." - ); - break; - } - - $currentUrl = $this->container->request->getUri()->getBaseUrl() . - $this->container->request->getUri()->getPath(); - - // Get Access Token - $token = $this->apiRequest( - $provider->token_url, - array( - 'client_id' => $this->container->settings['oauth2'][$provider->slug]['client_id'], - 'client_secret' => $this->container->settings['oauth2'][$provider->slug]['client_secret'], - 'redirect_uri' => $currentUrl, - 'code' => $this->container->request->getParam('code'), - 'grant_type' => 'authorization_code' - ), - array(), - true - ); - break; - } - - return $token; - } - - private function getTwitterAccessToken() - { - $twConnection = new TwitterOAuth( - $this->container->settings['oauth2']['twitter']['client_id'], - $this->container->settings['oauth2']['twitter']['client_secret'] - ); - - $twArray = array(); - try { - $accessToken = $twConnection->oauth( - "oauth/access_token", - [ - "oauth_token" => $this->container->request->getParam('oauth_token'), - "oauth_verifier" => $this->container->request->getParam('oauth_verifier') - ] - ); - $twArray['access_token'] = $accessToken['oauth_token']; - $twArray['token_secret'] = $accessToken['oauth_token_secret']; - $twArray['uid'] = $accessToken['user_id']; - $twArray['screen_name'] = $accessToken['screen_name']; - $twArray['expires_in'] = $accessToken['x_auth_expires']; - } catch (\Abraham\TwitterOAuth\TwitterOAuthException $e) { - $twArray['error'] = (object) array( - "message" => (string) "An error occured. Please try again."); - } - - $token = (object) $twArray; - - return $token; - } - - private function getFacebookAccessToken($provider) - { - $currentUrl = $this->container->request->getUri()->getBaseUrl() . - $this->container->request->getUri()->getPath(); - // Verify the state matches our stored state - $oauth2State = $this->container->session->get('oauth2-state'); - $returnedState = $this->container->request->getParam('state'); - if ((string) $oauth2State !== (string) $returnedState) { - $output['error'] = (object) array( - "message" => (string) "Oauth2 Error: Session state did not match. Please try again."); - return (object) $output; - } - // Get Access Token - $token = $this->apiRequest( - $provider->token_url, - array( - 'client_id' => $this->container->settings['oauth2'][$provider->slug]['client_id'], - 'client_secret' => $this->container->settings['oauth2'][$provider->slug]['client_secret'], - 'redirect_uri' => $currentUrl, - 'code' => $this->container->request->getParam('code'), - 'grant_type' => 'authorization_code' - ), - array(), - true - ); - - if (isset($token->access_token)) { - // Get Long Lived Access Token - $token = $this->apiRequest( - $provider->token_url, - array( - 'client_id' => $this->container->settings['oauth2'][$provider->slug]['client_id'], - 'client_secret' => $this->container->settings['oauth2'][$provider->slug]['client_secret'], - 'fb_exchange_token' => $token->access_token, - 'grant_type' => 'fb_exchange_token' - ), - array(), - true - ); - return $token; - } - - $output['error'] = (object) array( - "message" => (string) "An error occured. Please try again."); - return (object) $output; - } - - private function getGithubAccessToken($provider) - { - // Verify the state matches our stored state - $oauth2State = $this->container->session->get('oauth2-state'); - $returnedState = $this->container->request->getParam('state'); - - if ((string) $oauth2State !== (string) $returnedState) { - $output['error'] = (object) array( - "message" => (string) "Oauth2 Error: Session state did not match. Please try again."); - return (object) $output; - } - - // Get Github access token - $token = $this->apiRequest( - $provider->token_url, - array( - 'client_id' => $this->container->settings['oauth2'][$provider->slug]['client_id'], - 'client_secret' => $this->container->settings['oauth2'][$provider->slug]['client_secret'], - 'code' => $this->container->request->getParam('code'), - ), - array( - 'User-Agent: ' . $this->container->settings['oauth2'][$provider->slug]['app_name'], - 'Content-type: application/x-www-form-urlencoded' - ), - false - ); - - parse_str($token, $output); - $output['expires_in'] = 0; - $token = (object) $output; - - return $token; - } -} diff --git a/app/src/Dappurware/VideoParser.php b/app/src/Dappurware/VideoParser.php deleted file mode 100644 index 064ded1..0000000 --- a/app/src/Dappurware/VideoParser.php +++ /dev/null @@ -1,100 +0,0 @@ -