/** * Delete dynamic field files * * @param int $fieldId * @param int $modelId * @param string $model * @param boolean $encrypt * @param Application $app * @param string $uploadFolder * @param string $mediaLibrary * @return void */ protected static function saveFiles($fieldId, $modelId, $model, $encrypt, $app, $uploadFolder, $mediaLibrary = null) { $field = T\Fields::findById($fieldId); if (isset($field->id)) { $time = time(); $newValues = []; $oldValues = new Record(); $oldValues->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $oldValues->findRecordsBy(['model_id' => $modelId, 'model' => $model], ['order' => 'id ASC']); $old = $oldValues->rows(false); foreach ($_FILES as $key => $file) { $id = substr_count($key, '_') == 2 ? substr($key, strrpos($key, '_') + 1) : 0; if (!empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (null !== $mediaLibrary) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $old[$id]['value'])) { $media = new \Phire\Media\Model\Media(); $media->getByFile($old[$id]['value']); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } else { $newValues[] = $value; } copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $app->module('phire-fields')->config()['max_size'], $app->module('phire-fields')->config()['disallowed_types'], $app->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } } } else { $newValues[] = $value; } } } } foreach ($newValues as $v) { if (!empty($v)) { $fv = new Record(['model_id' => $modelId, 'model' => $model, 'timestamp' => $time, 'revision' => 0, 'value' => $v]); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->save(); $fvs = new Record(); $fvs->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $sql = $fvs->getSql(); $sql->update(['timestamp' => ':timestamp'])->where('model_id = :model_id')->where('model = :model'); $fvs->execute($sql, ['timestamp' => $time, 'model_id' => $modelId, 'model' => $model]); } } } }
/** * Upload template * * @param array $file * @return void */ public function upload($file) { $templatePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/templates'; if (!file_exists($templatePath)) { mkdir($templatePath); chmod($templatePath, 0777); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/index.html')) { copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/index.html', $templatePath . '/index.html'); chmod($templatePath . '/index.html', 0777); } } $upload = new Upload($templatePath); $template = $upload->upload($file); $formats = Archive::getFormats(); if (file_exists($templatePath . '/' . $template)) { $ext = null; $name = null; if (substr($template, -4) == '.zip') { $ext = 'zip'; $name = substr($template, 0, -4); } else { if (substr($template, -4) == '.tgz') { $ext = 'tgz'; $name = substr($template, 0, -4); } else { if (substr($template, -7) == '.tar.gz') { $ext = 'tar.gz'; $name = substr($template, 0, -7); } } } if (null !== $ext && null !== $name && array_key_exists($ext, $formats)) { $archive = new Archive($templatePath . '/' . $template); $archive->extract($templatePath); if (stripos($template, 'gz') !== false && file_exists($templatePath . '/' . $name . '.tar')) { unlink($templatePath . '/' . $name . '.tar'); } if (file_exists($templatePath . '/' . $name)) { $dir = new Dir($templatePath . '/' . $name, ['filesOnly' => true]); foreach ($dir->getFiles() as $file) { if (substr($file, -5) == '.html') { $isVisible = stripos($file, 'category') === false && stripos($file, 'error') === false && stripos($file, 'tag') === false && stripos($file, 'search') === false && stripos($file, 'sidebar') === false && stripos($file, 'header') === false && stripos($file, 'footer') === false; $template = new Table\Templates(['parent_id' => null, 'name' => ucwords(str_replace(['-', '_'], [' ', ' '], substr(strtolower($file), 0, -5))), 'device' => 'desktop', 'template' => file_get_contents($templatePath . '/' . $name . '/' . $file), 'history' => null, 'visible' => (int) $isVisible]); $template->save(); } } } } } }
/** * Upload theme * * @param array $file * @return void */ public function upload($file) { $folder = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes'; $upload = new Upload($folder); $upload->upload($file); }
/** * Save dynamic field values to the EAV table * * @param Application $application * @param Table\Fields $field * @param mixed $value * @param string $model * @param int $modelId * @param string $uploadFolder * @param string $mediaLibrary * @return void */ public static function save(Application $application, $field, $value, $model, $modelId, $uploadFolder = null, $mediaLibrary = null) { $dynamicFieldIds = []; $fieldId = $field->id; $key = 'field_' . $fieldId; if ($field->dynamic) { $dynamicFieldIds[] = $field->id; } $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if ($field->type == 'file' && isset($_FILES[$key]) && !empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (isset($fv->field_id)) { $oldFile = json_decode($fv->value); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile); } } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['disallowed_types'], $application->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); } } if (!empty($value) && $value != ' ') { if ($field->encrypt && !is_array($value)) { $value = (new Mcrypt())->create($value); } } if (isset($fv->field_id)) { $oldValue = json_decode($fv->value, true); if (!empty($value) && $value != ' ') { if (strpos($field->type, '-history') !== false) { if ($value != $oldValue) { $ts = null !== $fv->timestamp ? $fv->timestamp : time() - 180; if (null !== $fv->history) { $history = json_decode($fv->history, true); $history[$ts] = $oldValue; if (count($history) > $application->module('phire-fields')->config()['history']) { $history = array_slice($history, 1, $application->module('phire-fields')->config()['history'], true); } $fv->history = json_encode($history); } else { $fv->history = json_encode([$ts => $oldValue]); } } } if ($field->dynamic && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = $value; $newValue = json_encode($oldValue); } else { $newValue = json_encode($value); } $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } else { if (!$field->dynamic && $field->type != 'file') { $fv->delete(); } else { if ($field->dynamic && $field->type != 'file' && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = ''; $newValue = json_encode($oldValue); $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } } } } else { if (!empty($value) && $value != ' ') { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => $field->dynamic ? json_encode([$value]) : json_encode($value), 'timestamp' => time()]); $fv->save(); } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); $checkValue = json_decode($fv->value, true); if (is_array($checkValue) && isset($checkValue[0]) && is_array($checkValue[0])) { foreach ($checkValue as $k => $v) { $fieldToCheck = $k > 0 ? 'field_' . $fieldId . '_' . $k : 'field_' . $fieldId; if (!isset($_POST[$fieldToCheck])) { unset($checkValue[$k]); } } $checkValue = array_values($checkValue); $fv->value = json_encode($checkValue); $fv->timestamp = time(); $fv->save(); } while (isset($_POST['field_' . $fieldId . '_' . $i])) { if (!empty($_POST['field_' . $fieldId . '_' . $i]) && $_POST['field_' . $fieldId . '_' . $i] != ' ') { $postValue = $_POST['field_' . $fieldId . '_' . $i]; if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } else { if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i])) { unset($value[$i]); $value = array_values($value); $offset++; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); while (isset($_FILES['field_' . $fieldId . '_' . $i])) { if (!empty($_FILES['field_' . $fieldId . '_' . $i]['tmp_name'])) { if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES['field_' . $fieldId . '_' . $i])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES['field_' . $fieldId . '_' . $i], ['library_id' => $library->id]); $postValue = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['allowed_types']); $postValue = $upload->upload($_FILES['field_' . $fieldId . '_' . $i]); } if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (is_array($value) && isset($value[0]) && is_array($value[0])) { foreach ($value as $key => $val) { if (is_array($val) && isset($val[0]) && (empty($val[0]) || $val[0] == ' ')) { unset($val[0]); $value[$key] = array_values($val); if (count($value[$key]) == 0) { unset($value[$key]); } } } $value = array_values($value); } else { if (is_array($value) && isset($value[0]) && (empty($value[0]) || $value[0] == ' ')) { unset($value[0]); $value = array_values($value); } } if (count($value) == 0) { $fv->delete(); } else { $fv->value = json_encode($value); $fv->save(); } } } }
/** * Method to process the form * * @return self */ public function process() { $fields = $this->getFields(); $submission = new Table\FormSubmissions(['form_id' => $this->id, 'timestamp' => date('Y-m-d H:i:s'), 'ip_address' => $_SERVER['REMOTE_ADDR']]); $submission->save(); unset($fields['csrf']); unset($fields['captcha']); unset($fields['id']); unset($fields['submit']); $files = []; if ($_FILES) { foreach ($_FILES as $key => $value) { if (isset($value['tmp_name']) && !empty($value['tmp_name']) && class_exists('Phire\\Fields\\Model\\Field')) { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files'); $filename = $upload->checkFilename($value['name'], $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files'); $fields[$key] = $filename; $files[] = $filename; $upload->upload($value); unset($_FILES[$key]); } } } $fv = new \Phire\Fields\Model\FieldValue(); $values = $fv->save($fields, $submission->id, 'Phire\\Forms\\Model\\FormSubmission'); $form = Table\Forms::findById($this->id); // If the form action is set if (!empty($form->action)) { $scheme = $form->force_ssl ? 'https://' : 'http://'; $action = substr($form->action, 0, 4) == 'http' ? $form->action : $scheme . $_SERVER['HTTP_HOST'] . BASE_PATH . $form->action; if ($form->method == 'post') { $options = [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $values, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true]; } else { $action .= '?' . http_build_query($values); $options = [CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true]; } $curl = new \Pop\Http\Client\Curl($action, $options); $curl->send(); unset($curl); } // Send the submission if the form "to" field is set if (!empty($form->to)) { $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']); $subject = $form->name . ' : ' . $domain; // Set the recipient $rcpt = ['email' => $form->to]; $message = ''; foreach ($values as $key => $value) { $message .= ucwords(str_replace('_', ' ', $key)) . ': ' . (is_array($value) ? implode(', ', $value) : $value) . PHP_EOL; } // Send form submission $mail = new Mail($subject, $rcpt); if (!empty($form->from)) { if (!empty($form->reply_to)) { $mail->from($form->from, null, false)->replyTo($form->reply_to, null, false); } else { $mail->from($form->from); } } else { if (!empty($form->reply_to)) { $mail->replyTo($form->from); } else { $mail->from('noreply@' . $domain); } } $mail->setText($message); if (count($files) > 0) { foreach ($files as $file) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file)) { $mail->attachFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file); } } } $mail->send(); } $this->clear(); if (!empty($form->redirect)) { if (substr($form->redirect, 0, 4) == 'http' || substr($form->redirect, 0, 1) == '/') { $this->redirect = true; $redirect = substr($form->redirect, 0, 4) == 'http' ? $form->redirect : BASE_PATH . $form->redirect; header('Location: ' . $redirect); exit; } else { $this->message = $form->redirect; } } return $this; }
/** * Upload module * * @param array $file * @return void */ public function upload($file) { $folder = MODULES_ABS_PATH; $upload = new Upload($folder); $upload->upload($file); }