示例#1
0
 /**
  * JSON action method
  *
  * @return void
  */
 public function json()
 {
     $json = ['seo_title' => '', 'description' => '', 'keywords' => ''];
     $seoTitle = \Phire\Fields\Table\Fields::findBy(['name' => 'seo_title']);
     $description = \Phire\Fields\Table\Fields::findBy(['name' => 'description']);
     $keywords = \Phire\Fields\Table\Fields::findBy(['name' => 'keywords']);
     if (isset($seoTitle->id)) {
         $json['seo_title'] = $seoTitle->id;
     }
     if (isset($description->id)) {
         $json['description'] = $description->id;
     }
     if (isset($keywords->id)) {
         $json['keywords'] = $keywords->id;
     }
     $body = json_encode($json, JSON_PRETTY_PRINT);
     $this->send(200, ['Content-Type' => 'application/json'], $body);
 }
示例#2
0
 /**
  * Get view by ID
  *
  * @param  int $id
  * @return void
  */
 public function getById($id)
 {
     $view = Table\Views::findById($id);
     if (isset($view->id)) {
         $data = $view->getColumns();
         if (!empty($data['group_fields'])) {
             $data['group_fields'] = explode('|', $data['group_fields']);
             $data['_group_fields'] = [];
             $data['group_fields_names'] = [];
             foreach ($data['group_fields'] as $id) {
                 $data['_group_fields'][] = '_' . $id;
                 if (!is_numeric($id)) {
                     $data['group_fields_names'][] = $id;
                 } else {
                     $f = \Phire\Fields\Table\Fields::findById($id);
                     if (isset($f->id)) {
                         $data['group_fields_names'][] = $f->name;
                     }
                 }
             }
         }
         if (!empty($data['single_fields'])) {
             $data['single_fields'] = explode('|', $data['single_fields']);
             $data['_single_fields'] = [];
             $data['single_fields_names'] = [];
             foreach ($data['single_fields'] as $id) {
                 $data['_single_fields'][] = '_' . $id;
                 if (!is_numeric($id)) {
                     $data['single_fields_names'][] = $id;
                 } else {
                     $f = \Phire\Fields\Table\Fields::findById($id);
                     if (isset($f->id)) {
                         $data['single_fields_names'][] = $f->name;
                     }
                 }
             }
         }
         if (!empty($data['models'])) {
             $data['models'] = unserialize($data['models']);
         }
         $this->data = array_merge($this->data, $data);
     }
 }
示例#3
0
 /**
  * Bootstrap the module
  *
  * @param  Application $application
  * @return void
  */
 public static function bootstrap(Application $application)
 {
     if ($application->isRegistered('phire-content') && $application->isRegistered('phire-fields')) {
         $fields = \Phire\Fields\Table\Fields::findBy(['models' => "%Phire\\\\Content\\\\Model\\\\Content%"]);
         $names = [];
         foreach ($fields->rows() as $field) {
             $names[] = $field->name;
         }
         if (!in_array('seo_title', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'seo_title', 'label' => 'SEO Title', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -3, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
         if (!in_array('description', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'description', 'label' => 'Description', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -2, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
         if (!in_array('keywords', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'keywords', 'label' => 'Keywords', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -1, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
     }
 }
示例#4
0
 /**
  * Get field values for a model object
  *
  * @param  mixed $model
  * @param  int   $id
  * @param  array $filters
  * @return mixed
  */
 public static function getModelObjectValues($model, $id = null, array $filters = [])
 {
     if (is_string($model)) {
         $class = $model;
     } else {
         $class = get_class($model);
         if (isset($model->id)) {
             $id = $model->id;
         }
     }
     $fieldValues = [];
     $sql = Table\Fields::sql();
     $sql->select()->where('models LIKE :models');
     $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%' . $class . '%' : '%' . addslashes($class) . '%';
     $fields = Table\Fields::execute((string) $sql, ['models' => $value]);
     if (null !== $id && $fields->count() > 0) {
         foreach ($fields->rows() as $field) {
             $fValue = '';
             if ($field->storage == 'eav') {
                 $fv = Table\FieldValues::findById([$field->id, $id, $class]);
                 if (isset($fv->field_id)) {
                     $fValue = json_decode($fv->value);
                     foreach ($filters as $filter => $params) {
                         if (null !== $params && count($params) > 0) {
                             $params = array_merge([$fValue], $params);
                         } else {
                             $params = [$fValue];
                         }
                         $fValue = call_user_func_array($filter, $params);
                     }
                     if ($field->encrypt) {
                         if (is_array($fValue)) {
                             foreach ($fValue as $k => $fv) {
                                 $fValue = (new Mcrypt())->decrypt($fValue);
                             }
                         } else {
                             $fValue = (new Mcrypt())->decrypt($fValue);
                         }
                     }
                 }
             } else {
                 $fv = new Record();
                 $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                 $fv->findRecordsBy(['model_id' => $id, 'model' => $class, 'revision' => 0]);
                 if ($fv->hasRows()) {
                     if ($fv->count() > 1) {
                         $fValue = [];
                         foreach ($fv->rows() as $f) {
                             $fValue[] = $field->encrypt ? (new Mcrypt())->decrypt($f->value) : $f->value;
                         }
                     } else {
                         $fValue = $field->encrypt ? (new Mcrypt())->decrypt($fv->value) : $fv->value;
                     }
                 }
             }
             if (is_object($model)) {
                 $model->{$field->name} = self::parse($fValue);
             } else {
                 $fieldValues[$field->name] = self::parse($fValue);
             }
         }
     }
     return is_object($model) ? $model : $fieldValues;
 }
示例#5
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]);
             }
         }
     }
 }
示例#6
0
 /**
  * Drop field table
  *
  * @param  string $name
  * @return void
  */
 protected function dropFieldTable($name)
 {
     $sql = Table\Fields::sql();
     $db = $sql->getDb();
     $table = $sql->quoteId(DB_PREFIX . 'field_' . $name);
     $db->query('DROP TABLE ' . $table);
     $module = \Phire\Table\Modules::findBy(['folder' => 'phire-fields']);
     if (isset($module->id)) {
         $assets = unserialize($module->assets);
         if (in_array(DB_PREFIX . 'field_' . $name, $assets['tables'])) {
             unset($assets['tables'][array_search(DB_PREFIX . 'field_' . $name, $assets['tables'])]);
         }
         $module->assets = serialize($assets);
         $module->save();
     }
 }
示例#7
0
 /**
  * Process a form submission
  *
  * @param  array $fields
  * @return void
  */
 public function process(array $fields)
 {
     if (isset($fields['rm_submissions'])) {
         foreach ($fields['rm_submissions'] as $id) {
             $fv = \Phire\Fields\Table\FieldValues::findBy(['model_id' => $id]);
             foreach ($fv->rows() as $value) {
                 $field = \Phire\Fields\Table\Fields::findById($value->field_id);
                 if (isset($field->id) && $field->type == 'file') {
                     $file = json_decode($value->value);
                     if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file)) {
                         unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file);
                     }
                 }
             }
             $fv = new \Phire\Fields\Table\FieldValues();
             $fv->delete(['model_id' => $id]);
             $form = Table\FormSubmissions::findById((int) $id);
             if (isset($form->id)) {
                 $form->delete();
             }
         }
     }
 }
示例#8
0
 /**
  * Get all entity field names
  *
  * @param  array $rows
  * @return array
  */
 public function getAllFields(array $rows)
 {
     $fieldNames = [];
     foreach ($rows as $i => $row) {
         if (class_exists('Phire\\Fields\\Model\\FieldValue')) {
             $class = 'Phire\\Entities\\Model\\Entity';
             $sql = \Phire\Fields\Table\Fields::sql();
             $sql->select()->where('models LIKE :models');
             $sql->select()->orderBy('order');
             $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%' . $class . '%' : '%' . addslashes($class) . '%';
             $fields = \Phire\Fields\Table\Fields::execute((string) $sql, ['models' => $value]);
             foreach ($fields->rows() as $field) {
                 $field->models = unserialize($field->models);
                 if ($this->isFieldAllowed($field->models, $row)) {
                     if ($field->storage == 'eav') {
                         $fv = \Phire\Fields\Table\FieldValues::findBy(['field_id' => $field->id, 'model_id' => $row->id, 'model' => 'Phire\\Entities\\Model\\Entity']);
                         foreach ($fv->rows() as $fv) {
                             if (!array_key_exists($field->name, $fieldNames)) {
                                 $fieldNames[$field->name] = $field->type;
                             }
                             $rows[$i][$field->name] = json_decode($fv->value, true);
                         }
                     } else {
                         $fv = new \Pop\Db\Record();
                         $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                         $fv->findRecordsBy(['model_id' => $row->id, 'model' => 'Phire\\Entities\\Model\\Entity', 'revision' => 0]);
                         if (!array_key_exists($field->name, $fieldNames)) {
                             $fieldNames[$field->name] = $field->type;
                         }
                         if ($fv->count() > 1) {
                             $rows[$i][$field->name] = [];
                             foreach ($fv->rows() as $f) {
                                 $rows[$i][$field->name][] = $f->value;
                             }
                         } else {
                             $rows[$i][$field->name] = $fv->value;
                         }
                     }
                 }
             }
         }
     }
     return $fieldNames;
 }
示例#9
0
 /**
  * JSON models action method
  *
  * @param  mixed $model
  * @param  mixed $fid
  * @param  mixed $marked
  * @return void
  */
 public function json($model = null, $fid = null, $marked = null)
 {
     $json = [];
     // Get field validators and models
     if ($model == 0 && null !== $fid) {
         $field = Table\Fields::findById($fid);
         if (isset($field->id)) {
             $json['validators'] = null != $field->validators ? unserialize($field->validators) : [];
             $json['models'] = null != $field->models ? unserialize($field->models) : [];
         }
         // Get field values
     } else {
         if (null !== $fid && null == $marked && null !== $this->request->getQuery('model')) {
             $field = Table\Fields::findById($fid);
             if ($field->dynamic) {
                 if ($field->storage == 'eav') {
                     $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]);
                     if (!empty($fv->value)) {
                         $values = json_decode($fv->value, true);
                         if (is_array($values)) {
                             array_shift($values);
                         }
                     } else {
                         $values = [];
                     }
                 } else {
                     $fv = new Record();
                     $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                     $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model')], ['order' => 'id ASC']);
                     $values = [];
                     if ($fv->hasRows() && $fv->count() > 1) {
                         $rows = $fv->rows();
                         for ($i = 1; $i < count($rows); $i++) {
                             $values[] = $rows[$i]->value;
                         }
                     }
                 }
                 $json['values'] = $values;
             }
             // Get field history values
         } else {
             if (null !== $fid && null !== $marked && null !== $this->request->getQuery('model')) {
                 $field = Table\Fields::findById($fid);
                 $value = '';
                 if (isset($field->id)) {
                     if ($field->storage == 'eav') {
                         $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]);
                         if (isset($fv->field_id) && null !== $fv->history) {
                             $history = json_decode($fv->history, true);
                             if (isset($history[$marked])) {
                                 $value = $history[$marked];
                                 $f = Table\Fields::findById($fid);
                                 if ($f->encrypt) {
                                     $value = (new \Pop\Crypt\Mcrypt())->decrypt($value);
                                 }
                             }
                         }
                         $json['fieldId'] = $fid;
                         $json['modelId'] = $model;
                         $json['model'] = $this->request->getQuery('model');
                         $json['value'] = $value;
                     } else {
                         $fv = new Record();
                         $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                         $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model'), 'timestamp' => $marked], ['order' => 'id ASC']);
                         if (isset($fv->model_id)) {
                             $value = $fv->value;
                             if ($field->encrypt) {
                                 $value = (new \Pop\Crypt\Mcrypt())->decrypt($value);
                             }
                         }
                         $json['fieldId'] = $fid;
                         $json['modelId'] = $model;
                         $json['model'] = $this->request->getQuery('model');
                         $json['value'] = $value;
                     }
                 }
                 // Get field models
             } else {
                 $model = rawurldecode($model);
                 $models = $this->application->module('phire-fields')->config()['models'];
                 if (isset($models[$model])) {
                     $json = $models[$model];
                 }
             }
         }
     }
     $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT));
     $this->send(200, ['Content-Type' => 'application/json']);
 }
示例#10
0
 /**
  * Constructor method to instantiate the form object
  *
  * @param  mixed $id
  * @param  array $captchaConfig
  * @throws \Pop\Form\Exception
  * @return self
  */
 public function __construct($id, $captchaConfig = [])
 {
     $form = is_numeric($id) ? Table\Forms::findById($id) : Table\Forms::findBy(['name' => $id]);
     if (!isset($form->id)) {
         throw new \Pop\Form\Exception('That form does not exist.');
     }
     if (!class_exists('Phire\\Fields\\Model\\Field')) {
         throw new \Pop\Form\Exception('The phire-fields module is not installed or active.');
     }
     $fieldGroups = [];
     $submitAttributes = [];
     $formAttributes = [];
     $this->filter = (bool) $form->filter;
     if (!empty($form->submit_attributes)) {
         $attribs = explode('" ', $form->submit_attributes);
         foreach ($attribs as $attrib) {
             $attAry = explode('=', $attrib);
             $att = trim($attAry[0]);
             $val = str_replace('"', '', trim($attAry[1]));
             $submitAttributes[$att] = $val;
         }
     }
     if (!empty($form->attributes)) {
         $attribs = explode('" ', $form->attributes);
         foreach ($attribs as $attrib) {
             $attAry = explode('=', $attrib);
             $att = trim($attAry[0]);
             $val = str_replace('"', '', trim($attAry[1]));
             $formAttributes[$att] = $val;
         }
     }
     $sql = \Phire\Fields\Table\Fields::sql();
     $sql->select()->where('models LIKE :models');
     $sql->select()->orderBy('order', 'ASC');
     $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%Phire\\Forms\\Model\\Form%' : '%Phire\\\\Forms\\\\Model\\\\Form%';
     $fields = \Phire\Fields\Table\Fields::execute((string) $sql, ['models' => $value]);
     foreach ($fields->rows() as $field) {
         if (null !== $field->group_id) {
             $fieldGroups[$field->group_id] = [];
         }
     }
     $fieldGroups[0] = [];
     $fieldGroups[-1] = [];
     foreach ($fields->rows() as $field) {
         $field->validators = unserialize($field->validators);
         $field->models = unserialize($field->models);
         if (null !== $field->group_id) {
             foreach ($field->models as $model) {
                 if (null === $model['type_value'] || $form->id == $model['type_value']) {
                     $fieldConfig = \Phire\Fields\Event\Field::createFieldConfig($field);
                     if (strpos($fieldConfig['label'], '<span class="editor-link-span">') !== false) {
                         $fieldConfig['label'] = str_replace('<span class="editor-link-span">', '<span style="display: none;" class="editor-link-span">', $fieldConfig['label']);
                     }
                     $fieldGroups[$field->group_id]['field_' . $field->id] = $fieldConfig;
                     break;
                 }
             }
         } else {
             if (null === $field->group_id) {
                 foreach ($field->models as $model) {
                     if (null === $model['type_value'] || $form->id == $model['type_value']) {
                         $fieldConfig = \Phire\Fields\Event\Field::createFieldConfig($field);
                         if (strpos($fieldConfig['label'], '<span class="editor-link-span">') !== false) {
                             $fieldConfig['label'] = str_replace('<span class="editor-link-span">', '<span style="display: none;" class="editor-link-span">', $fieldConfig['label']);
                         }
                         $fieldGroups[0]['field_' . $field->id] = $fieldConfig;
                         break;
                     }
                 }
             }
         }
     }
     if ($form->use_csrf) {
         $fieldGroups[-1]['csrf'] = ['type' => 'csrf'];
     }
     if ($form->use_captcha) {
         if (class_exists('Phire\\Captcha\\Model\\Captcha')) {
             $captcha = new \Phire\Captcha\Model\Captcha($captchaConfig);
             $captcha->createToken();
             $fieldGroups[-1]['captcha'] = ['type' => 'captcha', 'label' => 'Enter Code', 'token' => $captcha->token];
         } else {
             $fieldGroups[-1]['captcha'] = ['type' => 'captcha', 'label' => 'Please Solve: '];
         }
     }
     $fieldGroups[-1]['id'] = ['type' => 'hidden', 'value' => $form->id];
     $fieldGroups[-1]['submit'] = ['type' => 'submit', 'label' => '&nbsp;', 'value' => !empty($form->submit_value) ? $form->submit_value : 'SUBMIT', 'attributes' => $submitAttributes];
     parent::__construct($fieldGroups, null, $form->method);
     foreach ($formAttributes as $attrib => $value) {
         $this->setAttribute($attrib, $value);
     }
 }
示例#11
0
 /**
  * JSON action method
  *
  * @param  int $id
  * @param  int $tid
  * @param  int $vid
  * @return void
  */
 public function json($id, $tid = null, $vid = null)
 {
     $json = [];
     if (is_numeric($id)) {
         $view = Table\Views::findById($id);
         if (isset($view->id)) {
             $json['models'] = null != $view->models ? unserialize($view->models) : [];
         }
     } else {
         $fields = \Phire\Fields\Table\Fields::findAll();
         $json['gMarked'] = [];
         $json['sMarked'] = [];
         $json['fields'] = ['_id' => 'id', '_title' => 'title'];
         foreach ($fields->rows() as $field) {
             $models = unserialize($field->models);
             foreach ($models as $model) {
                 if ($model['model'] == rawurldecode($id) && (null === $tid || null === $model['type_value'] || $model['type_value'] == $tid)) {
                     $json['fields']['_' . $field->id] = $field->name;
                 }
             }
         }
         if (null !== $vid) {
             $view = Table\Views::findById($vid);
             if (isset($view->id)) {
                 $json['gMarked'] = explode('|', $view->group_fields);
                 $json['sMarked'] = explode('|', $view->single_fields);
                 foreach ($json['gMarked'] as $k => $v) {
                     $json['gMarked'][$k] = '_' . $v;
                 }
                 foreach ($json['sMarked'] as $k => $v) {
                     $json['sMarked'][$k] = '_' . $v;
                 }
             }
         }
     }
     $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT));
     $this->send(200, ['Content-Type' => 'application/json']);
 }
示例#12
0
 /**
  * Process and save seo config
  *
  * @param  array $exclude
  * @return void
  */
 public function saveAnalysis(array $exclude = [])
 {
     $config = Table\Config::findById('seo_config');
     $cfg = isset($config->value) && !empty($config->value) && $config->value != '' ? unserialize($config->value) : [];
     $analysis = ['tracking' => false, 'sitemap' => false, 'robots' => false, 'caching' => false, 'site-verify' => false, 'content' => ['good' => [], 'bad' => []]];
     if (!empty($cfg['tracking']) && $cfg['tracking'] != '') {
         $analysis['tracking'] = true;
     }
     $sitemap = \Phire\Table\Modules::findBy(['name' => 'phire-sitemap']);
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/sitemap.xml') || isset($sitemap->id) && $sitemap->active) {
         $analysis['sitemap'] = true;
     }
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/robots.txt') || isset($cfg['robots']) && !empty($cfg['robots']) && $cfg['robots'] != '') {
         $analysis['robots'] = true;
     }
     $cacheDetect = false;
     $curl = new Curl('http://' . $_SERVER['HTTP_HOST'] . BASE_PATH);
     if ($curl->getCode() == 200 && null !== $curl->getHeader('Cache-Control') && null !== $curl->getHeader('Expires') && null !== $curl->getHeader('Last-Modified') && null !== $curl->getHeader('Etag')) {
         $cacheDetect = true;
     }
     $cache = \Phire\Table\Modules::findBy(['name' => 'phire-cache']);
     if ($cacheDetect || isset($cache->id) && $cache->active) {
         $analysis['caching'] = true;
     }
     $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH, ['relative' => true, 'filesOnly' => true]);
     $googleFileDetect = false;
     foreach ($dir->getFiles() as $file) {
         if (substr($file, 0, 6) == 'google' && strpos(file_get_contents($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/' . $file), 'google-site-verification') !== false) {
             $googleFileDetect = true;
         }
     }
     $googleMetaDetect = false;
     if (isset($cfg['meta'])) {
         foreach ($cfg['meta'] as $meta) {
             if ($meta['name'] == 'google-site-verification') {
                 $googleMetaDetect = true;
             }
         }
     }
     if ($googleFileDetect || $googleMetaDetect) {
         $analysis['site-verify'] = true;
     } else {
         if (function_exists('dns_get_record') && $_SERVER['HTTP_HOST'] != 'localhost') {
             $dns = dns_get_record($_SERVER['HTTP_HOST'], DNS_TXT);
             if (count($dns) > 0) {
                 foreach ($dns as $record) {
                     if (isset($record['txt']) && strpos($record['txt'], 'google-site-verification') !== false) {
                         $analysis['site-verify'] = true;
                     }
                 }
             }
         }
     }
     $fields = ['seo_title' => '', 'description' => '', 'keywords' => ''];
     $seoTitle = \Phire\Fields\Table\Fields::findBy(['name' => 'seo_title']);
     $description = \Phire\Fields\Table\Fields::findBy(['name' => 'description']);
     $keywords = \Phire\Fields\Table\Fields::findBy(['name' => 'keywords']);
     if (isset($seoTitle->id)) {
         $fields['seo_title'] = $seoTitle->id;
     }
     if (isset($description->id)) {
         $fields['description'] = $description->id;
     }
     if (isset($keywords->id)) {
         $fields['keywords'] = $keywords->id;
     }
     $content = \Phire\Content\Table\Content::findAll();
     foreach ($content->rows() as $c) {
         if (!in_array($c->type_id, $exclude)) {
             $seoTitle = '';
             $metaDesc = '';
             $metaKeys = '';
             if ($fields['seo_title'] != '') {
                 $seoTitleField = \Phire\Fields\Table\FieldValues::findById([$fields['seo_title'], $c->id, "Phire\\Content\\Model\\Content"]);
                 if (isset($seoTitleField->field_id)) {
                     $seoTitle = json_decode($seoTitleField->value);
                 }
             }
             if ($fields['description'] != '') {
                 $descriptionField = \Phire\Fields\Table\FieldValues::findById([$fields['description'], $c->id, "Phire\\Content\\Model\\Content"]);
                 if (isset($descriptionField->field_id)) {
                     $metaDesc = json_decode($descriptionField->value);
                 }
             }
             if ($fields['keywords'] != '') {
                 $keywordsField = \Phire\Fields\Table\FieldValues::findById([$fields['keywords'], $c->id, "Phire\\Content\\Model\\Content"]);
                 if (isset($keywordsField->field_id)) {
                     $metaKeys = json_decode($keywordsField->value);
                 }
             }
             if (strlen($seoTitle) > 0 && strlen($seoTitle) <= 60 && strlen($metaDesc) > 0 && strlen($metaDesc) <= 160 && strlen($metaKeys) > 0 && strlen($metaKeys) <= 255) {
                 $analysis['content']['good'][$c->id] = ['title' => $c->title, 'uri' => $c->uri];
             } else {
                 $analysis['content']['bad'][$c->id] = ['title' => $c->title, 'type_id' => $c->type_id, 'uri' => $c->uri, 'issues' => []];
                 if (strlen($seoTitle) == 0) {
                     $analysis['content']['bad'][$c->id]['issues'][] = 'No SEO Title';
                 } else {
                     if (strlen($seoTitle) > 60) {
                         $analysis['content']['bad'][$c->id]['issues'][] = 'SEO Title is too long';
                     }
                 }
                 if (strlen($metaDesc) == 0) {
                     $analysis['content']['bad'][$c->id]['issues'][] = 'No description meta tag';
                 } else {
                     if (strlen($metaDesc) > 160) {
                         $analysis['content']['bad'][$c->id]['issues'][] = 'Description meta tag is too long';
                     }
                 }
                 if (strlen($metaKeys) == 0) {
                     $analysis['content']['bad'][$c->id]['issues'][] = 'No keywords meta tag';
                 } else {
                     if (strlen($metaKeys) > 255) {
                         $analysis['content']['bad'][$c->id]['issues'][] = 'Keywords meta tag is too long';
                     }
                 }
             }
         }
     }
     $config = Table\Config::findById('seo_analysis');
     $config->value = serialize($analysis);
     $config->save();
 }
示例#13
0
 /**
  * Delete dynamic field values
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  *
  * @return void
  */
 public static function delete(AbstractController $controller, Application $application)
 {
     if ($_POST) {
         $uploadFolder = BASE_PATH . CONTENT_PATH . '/files';
         $mediaLibrary = $application->module('phire-fields')->config()['media_library'];
         foreach ($_POST as $key => $value) {
             if (substr($key, 0, 3) == 'rm_' && is_array($value)) {
                 $fields = Table\Fields::findBy();
                 $fieldIds = [];
                 $fieldTypes = [];
                 $fieldStorage = [];
                 foreach ($fields->rows() as $field) {
                     $fieldIds[$field->id] = $field->name;
                     $fieldTypes[$field->id] = $field->type;
                     $fieldStorage[$field->id] = $field->storage;
                 }
                 foreach ($value as $id) {
                     foreach ($fieldIds as $fieldId => $fieldName) {
                         if ($fieldStorage[$fieldId] == 'eav') {
                             $fv = Table\FieldValues::findBy(['model_id' => (int) $id]);
                             if ($fv->hasRows()) {
                                 foreach ($fv->rows() as $f) {
                                     $fValue = json_decode($f->value, true);
                                     if (!is_array($fValue)) {
                                         $fValue = [$fValue];
                                     }
                                     foreach ($fValue as $f) {
                                         if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f)) {
                                             unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f);
                                         }
                                         if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
                                             $library = new \Phire\Media\Model\MediaLibrary();
                                             $library->getByFolder($mediaLibrary);
                                             if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f)) {
                                                 $media = new \Phire\Media\Model\Media();
                                                 $media->getByFile($f);
                                                 if (isset($media->id)) {
                                                     $media->remove(['rm_media' => [$media->id]]);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             $fv = new Table\FieldValues();
                             $fv->delete(['model_id' => (int) $id]);
                         } else {
                             $fv = new Record();
                             $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $fieldName);
                             $fv->findRecordsBy(['model_id' => (int) $id]);
                             if ($fieldTypes[$fieldId] == 'file' && $fv->hasRows()) {
                                 foreach ($fv->rows() as $f) {
                                     if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value)) {
                                         unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value);
                                     }
                                     if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
                                         $library = new \Phire\Media\Model\MediaLibrary();
                                         $library->getByFolder($mediaLibrary);
                                         if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f->value)) {
                                             $media = new \Phire\Media\Model\Media();
                                             $media->getByFile($f->value);
                                             if (isset($media->id)) {
                                                 $media->remove(['rm_media' => [$media->id]]);
                                             }
                                         }
                                     }
                                 }
                             }
                             $fv->delete(['model_id' => (int) $id]);
                         }
                     }
                 }
             }
         }
     }
 }
示例#14
0
 /**
  * Copy form
  *
  * @param  int $id
  * @param  \Pop\Module\Manager $modules
  * @return void
  */
 public function copy($id, \Pop\Module\Manager $modules = null)
 {
     $oldForm = Table\Forms::findById((int) $id);
     if (isset($oldForm->id)) {
         $i = 1;
         $name = $oldForm->name . ' (Copy ' . $i . ')';
         $dupeForm = Table\forms::findBy(['name' => $name]);
         while (isset($dupeForm->id)) {
             $i++;
             $name = $oldForm->name . ' (Copy ' . $i . ')';
             $dupeForm = Table\forms::findBy(['name' => $name]);
         }
         $form = new Table\Forms(['name' => $name, 'method' => !empty($oldForm->method) ? $oldForm->method : null, 'to' => !empty($oldForm->to) ? $oldForm->to : null, 'from' => !empty($oldForm->from) ? $oldForm->from : null, 'reply_to' => !empty($oldForm->reply_to) ? $oldForm->reply_to : null, 'action' => !empty($oldForm->action) ? $oldForm->action : null, 'redirect' => !empty($oldForm->redirect) ? $oldForm->redirect : null, 'attributes' => !empty($oldForm->attributes) ? $oldForm->attributes : null, 'submit_value' => !empty($oldForm->submit_value) ? $oldForm->submit_value : null, 'submit_attributes' => !empty($oldForm->submit_attributes) ? $oldForm->submit_attributes : null, 'use_captcha' => !empty($oldForm->use_captcha) ? (int) $oldForm->use_captcha : null, 'use_csrf' => !empty($oldForm->use_csrf) ? (int) $oldForm->use_csrf : null, 'force_ssl' => !empty($oldForm->force_ssl) ? (int) $oldForm->force_ssl : null]);
         $form->save();
         $flds = null;
         if (null !== $modules && $modules->isRegistered('phire-fields')) {
             $flds = \Phire\Fields\Table\Fields::findAll();
         }
         if (null !== $flds) {
             foreach ($flds->rows() as $f) {
                 if (!empty($f->models)) {
                     $models = unserialize($f->models);
                     print_r($models);
                     foreach ($models as $model) {
                         if ($model['model'] == 'Phire\\Forms\\Model\\Form' && $oldForm->id == $model['type_value']) {
                             $models[] = ['model' => 'Phire\\Forms\\Model\\Form', 'type_field' => 'id', 'type_value' => $form->id];
                             $newField = \Phire\Fields\Table\Fields::findById($f->id);
                             if (isset($newField->id)) {
                                 $newField->models = serialize($models);
                                 $newField->save();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
示例#15
0
 /**
  * Add dynamic fields to the form configs
  *
  * @param  Application $application
  * @return void
  */
 public static function addFields(Application $application)
 {
     $forms = $application->config()['forms'];
     $fields = Table\Fields::findBy(['group_id' => null], ['order' => 'order'], Table\FieldGroups::ROW_AS_ARRAYOBJECT);
     $groups = Table\FieldGroups::findAll(['order' => 'order'], Table\FieldGroups::ROW_AS_ARRAYOBJECT);
     if ($fields->count() > 0) {
         foreach ($fields->rows() as $field) {
             $field->validators = unserialize($field->validators);
             $field->models = unserialize($field->models);
             foreach ($field->models as $i => $model) {
                 if ($model['model'] == 'Phire\\Model\\User') {
                     $register = $model;
                     $registerEmail = $model;
                     $profile = $model;
                     $profileEmail = $model;
                     $register['model'] = 'Phire\\Model\\Register';
                     $registerEmail['model'] = 'Phire\\Model\\RegisterEmail';
                     $profile['model'] = 'Phire\\Model\\Profile';
                     $profileEmail['model'] = 'Phire\\Model\\ProfileEmail';
                     $field->models[] = $register;
                     $field->models[] = $registerEmail;
                     $field->models[] = $profile;
                     $field->models[] = $profileEmail;
                 }
             }
             foreach ($field->models as $model) {
                 $form = str_replace('Model', 'Form', $model['model']);
                 if (isset($forms[$form]) && self::isAllowed($model, $application)) {
                     end($forms[$form]);
                     $key = key($forms[$form]);
                     reset($forms[$form]);
                     $fieldConfig = self::createFieldConfig($field);
                     if ($form == 'Phire\\Form\\Register' || $form == 'Phire\\Form\\RegisterEmail' || $form == 'Phire\\Form\\Profile' || $form == 'Phire\\Form\\ProfileEmail') {
                         $forms[$form][1]['field_' . $field->id] = $fieldConfig;
                     } else {
                         if ($field->dynamic) {
                             if (isset($fieldConfig['label'])) {
                                 $fieldConfig['label'] = '<a href="#" onclick="return phire.addField(' . $field->id . ');">[+]</a> ' . $fieldConfig['label'];
                             } else {
                                 $fieldConfig['label'] = '<a href="#" onclick="return phire.addField(' . $field->id . ');">[+]</a>';
                             }
                             if (isset($fieldConfig['attributes'])) {
                                 $fieldConfig['attributes']['data-path'] = BASE_PATH . APP_URI;
                                 $fieldConfig['attributes']['data-model'] = $model['model'];
                             } else {
                                 $fieldConfig['attributes'] = ['data-path' => BASE_PATH . APP_URI, 'data-model' => $model['model']];
                             }
                         }
                         if (is_numeric($key)) {
                             if ($field->prepend) {
                                 $forms[$form][$key] = array_merge(['field_' . $field->id => $fieldConfig], $forms[$form][$key]);
                             } else {
                                 $forms[$form][$key]['field_' . $field->id] = $fieldConfig;
                             }
                         } else {
                             if ($field->prepend) {
                                 $forms[$form] = array_merge(['field_' . $field->id => $fieldConfig], $forms[$form]);
                             } else {
                                 $forms[$form]['field_' . $field->id] = $fieldConfig;
                             }
                         }
                     }
                 }
             }
         }
     }
     $fieldGroups = [];
     $groupPrepend = [];
     if ($groups->count() > 0) {
         $tab = 1001;
         foreach ($groups->rows() as $group) {
             $groupPrepend[$group->id] = (bool) $group->prepend;
             $fields = Table\Fields::findBy(['group_id' => $group->id], ['order' => 'order']);
             if ($fields->count() > 0) {
                 $i = 0;
                 foreach ($fields->rows() as $field) {
                     $field->validators = unserialize($field->validators);
                     $field->models = unserialize($field->models);
                     foreach ($field->models as $model) {
                         $form = str_replace('Model', 'Form', $model['model']);
                         if (isset($forms[$form]) && self::isAllowed($model, $application)) {
                             $fieldConfig = self::createFieldConfig($field);
                             if ($field->dynamic) {
                                 if (isset($fieldConfig['label'])) {
                                     $fieldConfig['label'] = '<a href="#" onclick="return phire.addField(' . $field->id . ');">[+]</a> ' . $fieldConfig['label'];
                                 } else {
                                     $fieldConfig['label'] = '<a href="#" onclick="return phire.addField(' . $field->id . ']);">[+]</a>';
                                 }
                             }
                             if (isset($fieldConfig['attributes'])) {
                                 $fieldConfig['attributes']['tabindex'] = $tab;
                                 $fieldConfig['attributes']['data-path'] = BASE_PATH . APP_URI;
                                 $fieldConfig['attributes']['data-model'] = $model['model'];
                             } else {
                                 $fieldConfig['attributes'] = ['tabindex' => $tab, 'data-path' => BASE_PATH . APP_URI, 'data-model' => $model['model']];
                             }
                             $tab++;
                             if (!isset($fieldGroups[$form])) {
                                 $fieldGroups[$form] = [];
                             }
                             if (!isset($fieldGroups[$form][$field->group_id])) {
                                 $fieldGroups[$form][$field->group_id] = [];
                             }
                             if ($field->prepend) {
                                 $fieldGroups[$form][$field->group_id] = array_merge(['field_' . $field->id => $fieldConfig], $fieldGroups[$form][$field->group_id]);
                             } else {
                                 $fieldGroups[$form][$field->group_id]['field_' . $field->id] = $fieldConfig;
                             }
                         }
                     }
                     $i++;
                 }
             }
         }
     }
     foreach ($fieldGroups as $form => $configs) {
         $keys = array_keys($forms[$form]);
         $numeric = true;
         foreach ($keys as $key) {
             if (!is_numeric($key)) {
                 $numeric = false;
             }
         }
         $formConfig = $numeric ? $forms[$form] : [$forms[$form]];
         foreach ($configs as $id => $config) {
             if ($groupPrepend[$id]) {
                 $formConfig = array_merge($config, $formConfig);
             } else {
                 $formConfig[] = $config;
             }
         }
         $forms[$form] = $formConfig;
     }
     $application->mergeConfig(['forms' => $forms], true);
 }