Exemplo n.º 1
0
 /**
  * Set the field values
  *
  * @param  array $values
  * @param  array $settings
  * @return Media
  */
 public function setFieldValues(array $values = null, array $settings = [])
 {
     parent::setFieldValues($values);
     if ($_POST && $_FILES && count($settings) == 4) {
         $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
         if (!$upload->test($_FILES['file'])) {
             $this->getElement('file')->addValidator(new Validator\NotEqual($this->file, $upload->getErrorMessage()));
         }
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Set the field values
  *
  * @param  array $values
  * @param  array $settings
  * @return Media
  */
 public function setFieldValues(array $values = null, array $settings = [])
 {
     parent::setFieldValues($values);
     if ($_POST && $_FILES && count($settings) == 4) {
         $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
         foreach ($_FILES as $file) {
             if (!empty($file['name'])) {
                 if (!$upload->test($file)) {
                     $this->getElement('error')->addValidator(new Validator\NotEqual('1', $upload->getErrorMessage() . ' (' . $file['name'] . ')'));
                 }
             }
         }
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * 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]);
             }
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Browser action method
  *
  * @param  int $lid
  * @return void
  */
 public function browser($lid = null)
 {
     if (null !== $this->request->getQuery('editor') && null !== $this->request->getQuery('type')) {
         $this->prepareView('media/browser.phtml');
         $this->view->title = 'Media Browser';
         if ($this->request->isPost()) {
             $library = new Model\MediaLibrary();
             $library->getById($lid);
             $settings = $library->getSettings();
             if (count($settings) == 4) {
                 $upload = new \Pop\File\Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
                 if ($upload->test($_FILES['file'])) {
                     $media = new Model\Media();
                     $media->save($_FILES['file'], $this->request->getPost());
                     $this->sess->setRequestValue('saved', true);
                     $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']));
                 } else {
                     $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']) . '&error=1');
                 }
             }
         }
         if (null !== $lid && null !== $this->request->getQuery('asset') && null !== $this->request->getQuery('asset_type')) {
             $assets = [];
             $limit = $this->config->pagination;
             $page = $this->request->getQuery('page');
             $pages = null;
             $library = new Model\MediaLibrary();
             if ($this->request->getQuery('asset_type') == 'content' && $this->application->isRegistered('phire-content')) {
                 $type = \Phire\Content\Table\ContentTypes::findById($lid);
                 $content = \Phire\Content\Table\Content::findBy(['type_id' => $lid], ['order' => 'order, id ASC']);
                 foreach ($content->rows() as $c) {
                     $assets[] = ['id' => $c->id, 'title' => $c->title, 'uri' => BASE_PATH . $c->uri];
                 }
                 if (isset($type->id)) {
                     $this->view->assetType = $type->name;
                 }
             } else {
                 if ($this->request->getQuery('asset_type') == 'media') {
                     $library->getById($lid);
                     $media = new Model\Media(['lid' => $lid]);
                     if ($this->request->getQuery('asset') == 'file') {
                         $assets = $media->getAll();
                     } else {
                         if ($this->request->getQuery('asset') == 'image') {
                             $assets = $media->getAllImages();
                         }
                     }
                     $this->view->assetType = $library->name;
                 }
             }
             if (count($assets) > $limit) {
                 $pages = new Paginator(count($assets), $limit);
                 $pages->useInput(true);
                 $offset = null !== $page && (int) $page > 1 ? $page * $limit - $limit : 0;
                 $assets = array_slice($assets, $offset, $limit, true);
             }
             $this->view->title = 'Media' . (null !== $this->view->assetType ? ' : ' . $this->view->assetType : null);
             $this->view->lid = $lid;
             $this->view->folder = $library->folder;
             $this->view->sizes = null !== $library->actions ? array_keys($library->actions) : [];
             $this->view->pages = $pages;
             $this->view->browserAssets = $assets;
         } else {
             $libraries = [];
             $limit = null;
             $pages = null;
             if ($this->request->getQuery('type') == 'file' && $this->application->isRegistered('phire-content')) {
                 $types = \Phire\Content\Table\ContentTypes::findAll(['order' => 'order ASC']);
                 if ($types->hasRows()) {
                     $libraries['Content'] = [];
                     foreach ($types->rows() as $type) {
                         $libraries['Content'][$type->id] = $type->name;
                     }
                 }
             }
             $libraries['Media'] = [];
             $library = new Model\MediaLibrary();
             $libs = $library->getAll();
             foreach ($libs as $lib) {
                 $libraries['Media'][$lib->id] = $lib->name;
             }
             $this->view->title = 'Media';
             $this->view->pages = $pages;
             $this->view->lid = $lid;
             $this->view->libraries = $libraries;
         }
         $this->send();
     } else {
         $this->redirect(BASE_PATH . APP_URI . '/media');
     }
 }
Exemplo n.º 5
0
 /**
  * Ajax action method
  *
  * @param  int $lid
  * @return void
  */
 public function ajax($lid)
 {
     $library = new Model\MediaLibrary();
     $library->getById($lid);
     $json = [];
     $categories = [];
     if (class_exists('Phire\\Categories\\Model\\Category') && isset($_POST['categories'])) {
         $categories = explode(',', $_POST['categories']);
     }
     if (!isset($library->id)) {
         $json['error'] = 'That library was not found.';
     } else {
         $settings = $library->getSettings();
         $folder = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder;
         if (!file_exists($folder)) {
             $library->createFolder($library->folder);
         }
         $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
         foreach ($_FILES as $key => $file) {
             if (!empty($file['name'])) {
                 $json['id'] = substr($key, strrpos($key, '_') + 1);
                 if (!$upload->test($file)) {
                     $json['error'] = $upload->getErrorMessage();
                 } else {
                     $media = new Model\Media();
                     $media->save($file, ['library_id' => $lid]);
                     if (count($categories) > 0) {
                         foreach ($categories as $category) {
                             $catItem = new \Phire\Categories\Table\CategoryItems(['category_id' => (int) $category, 'content_id' => null, 'media_id' => $media->id, 'order' => 0]);
                             $catItem->save();
                         }
                     }
                 }
             }
         }
     }
     $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT));
     $this->send(200, ['Content-Type' => 'application/json']);
 }
Exemplo n.º 6
0
 /**
  * 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();
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 /**
  * 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);
 }
Exemplo n.º 8
0
 /**
  * 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();
             }
         }
     }
 }
Exemplo n.º 9
0
 /**
  * Process batch archive file
  *
  * @param  string $file
  * @param  array  $fields
  * @return void
  */
 public function processBatch($file, array $fields)
 {
     $tmp = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp';
     mkdir($tmp);
     chmod($tmp, 0777);
     $batchFileName = (new Upload($tmp))->upload($file);
     $archive = new Archive($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName);
     $archive->extract($tmp);
     if (stripos($archive->getFilename(), '.tar') !== false && $archive->getFilename() != $batchFileName && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename())) {
         unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename());
     }
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName)) {
         unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName);
     }
     $library = new MediaLibrary();
     $library->getById($fields['library_id']);
     $settings = $library->getSettings();
     $dir = new Dir($tmp, ['absolute' => true, 'recursive' => true, 'filesOnly' => true]);
     $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
     foreach ($dir->getFiles() as $file) {
         $basename = basename($file);
         $testFile = ['name' => $basename, 'size' => filesize($file), 'error' => 0];
         if ($upload->test($testFile)) {
             $fileName = $upload->checkFilename($basename);
             copy($file, $settings['folder'] . '/' . $fileName);
             $title = ucwords(str_replace(['_', '-'], [' ', ' '], substr($fileName, 0, strrpos($fileName, '.'))));
             if (null !== $library->adapter) {
                 $class = 'Pop\\Image\\' . $library->adapter;
                 $formats = array_keys($class::getFormats());
                 $fileParts = pathinfo($fileName);
                 if (!empty($fileParts['extension']) && in_array(strtolower($fileParts['extension']), $formats)) {
                     $this->processImage($fileName, $library);
                 }
             }
             $media = new Table\Media(['library_id' => $fields['library_id'], 'title' => $title, 'file' => $fileName, 'size' => filesize($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder . DIRECTORY_SEPARATOR . $fileName), 'uploaded' => date('Y-m-d H:i:s'), 'order' => 0]);
             $media->save();
             $this->data['ids'][] = $media->id;
         }
     }
     $dir->emptyDir(true);
 }
Exemplo n.º 10
0
 /**
  * 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;
 }
Exemplo n.º 11
0
 /**
  * Upload module
  *
  * @param  array $file
  * @return void
  */
 public function upload($file)
 {
     $folder = MODULES_ABS_PATH;
     $upload = new Upload($folder);
     $upload->upload($file);
 }