Example #1
0
 public static function replacer($content = '', $data = array(), $other = array())
 {
     $other = (array) $other;
     $exploder = isset($other['exploder']) ? $other['exploder'] : '.';
     $replace_null = isset($other['replace_null']) ? $other['replace_null'] : false;
     $brackets = isset($other['brackets']) ? $other['brackets'] : array("{", "}");
     $op = $brackets[0];
     $cl = $brackets[1];
     preg_match_all('/' . preg_quote($op) . '([^(' . $cl . '|' . $op . '| )]*?)' . preg_quote($cl) . '/i', $content, $curly_matches);
     if (isset($curly_matches[1]) && !empty($curly_matches[1])) {
         foreach ($curly_matches[1] as $match) {
             $value = Arr::getVal($data, explode($exploder, $match));
             if (is_string($value) and !empty($other['nl2br'])) {
                 $value = nl2br($value);
             }
             if (!is_null($value)) {
                 if (is_array($value)) {
                     $content = str_replace($op . $match . $cl, var_export($value, true), $content);
                 } else {
                     if (!empty($other['escape'])) {
                         $content = str_replace($op . $match . $cl, htmlspecialchars($value), $content);
                     } else {
                         $content = str_replace($op . $match . $cl, $value, $content);
                     }
                 }
             } else {
                 if ($replace_null === true) {
                     $content = str_replace($op . $match . $cl, '', $content);
                 }
             }
         }
     }
     return $content;
 }
Example #2
0
 function get($k, $v = null)
 {
     $return = Arr::getVal($this->params, explode('.', $k), '___NOT_SET___');
     if (!is_string($return) or $return != '___NOT_SET___') {
         return $return;
     } else {
         return $v;
     }
 }
Example #3
0
 public static function replacer($content = '', $data = array(), $other = array())
 {
     $other = (array) $other;
     $exploder = isset($other['exploder']) ? $other['exploder'] : '.';
     $replace_null = isset($other['replace_null']) ? $other['replace_null'] : false;
     $repeater = isset($other['repeater']) ? $other['repeater'] : false;
     $brackets = isset($other['brackets']) ? $other['brackets'] : array("{", "}");
     $op = $brackets[0];
     $cl = $brackets[1];
     if (!empty($repeater)) {
         preg_match_all('/' . preg_quote($op) . $repeater . '([^' . preg_quote($cl) . ']*)' . preg_quote($cl) . '(.*?)' . preg_quote($op) . '\\/' . $repeater . preg_quote($cl) . '/is', $content, $repeater_matches);
         if (!empty($repeater_matches[0])) {
             foreach ($repeater_matches[0] as $k => $match) {
                 $path = '';
                 if (!empty($repeater_matches[1][$k])) {
                     $path = str_replace(':', '', $repeater_matches[1][$k]);
                 }
                 $sub_data = Arr::getVal($data, explode('.', $path), array());
                 if (!Arr::is_assoc($sub_data)) {
                     $sub_content = '';
                     foreach ($sub_data as $i => $sub_data_array) {
                         $sub_content .= self::replacer($repeater_matches[2][$k], $sub_data_array);
                     }
                     $content = str_replace($repeater_matches[0][$k], $sub_content, $content);
                 }
             }
         }
     }
     preg_match_all('/' . preg_quote($op) . '([^(' . $cl . '|' . $op . '| )]*?)' . preg_quote($cl) . '/i', $content, $curly_matches);
     if (isset($curly_matches[1]) && !empty($curly_matches[1])) {
         foreach ($curly_matches[1] as $match) {
             $value = Arr::getVal($data, explode($exploder, $match));
             if (is_string($value) and !empty($other['nl2br'])) {
                 $value = nl2br($value);
             }
             if (!is_null($value)) {
                 if (is_array($value)) {
                     $content = str_replace($op . $match . $cl, var_export($value, true), $content);
                 } else {
                     if (!empty($other['escape'])) {
                         $content = str_replace($op . $match . $cl, htmlspecialchars($value), $content);
                     } else {
                         $content = str_replace($op . $match . $cl, $value, $content);
                     }
                 }
             } else {
                 if ($replace_null === true) {
                     $content = str_replace($op . $match . $cl, '', $content);
                 }
             }
         }
     }
     return $content;
 }
Example #4
0
 public static function data($key, $default = null)
 {
     //check POST
     $value = Arr::getVal(self::$data, explode('.', $key), null);
     if (!is_null($value)) {
         return $value;
     }
     //check POST
     $post = Arr::getVal($_POST, explode('.', $key), null);
     if (!is_null($post)) {
         return $post;
     }
     //check GET
     $get = Arr::getVal($_GET, explode('.', $key), null);
     if (!is_null($get)) {
         return $get;
     }
     //return default
     return $default;
 }
Example #5
0
 public static function login($credentials)
 {
     $session = Base::getSession();
     $username_field = Base::getConfig('username_field', 'username');
     if (isset($credentials[$username_field]) and isset($credentials['password'])) {
         $user_model = new \GCore\Admin\Models\User();
         $user = $user_model->find('first', array('conditions' => array($username_field => $credentials[$username_field])));
         if (!empty($user)) {
             $user_groups = Arr::getVal($user, array('GroupUser', '[n]', 'group_id'), self::get_public_groups());
             $user_groups_paths = Arr::getVal($user, array('Group', '[n]', 'path'), array());
             $user_inheritance = array();
             foreach ($user_groups_paths as $user_groups_path) {
                 $user_inheritance = array_merge($user_inheritance, array_filter(explode('.', $user_groups_path)));
             }
             $user_inheritance = array_unique($user_inheritance);
             $user = $user['User'];
             $password_correct = self::check_password($credentials['password'], $user['password']);
             if (!$password_correct) {
                 $session->setFlash('error', l_('AUTHENTICATE_INCORRECT_LOGIN_CREDENTIALS'));
                 return false;
             }
             if (!empty($user['activation'])) {
                 $session->setFlash('error', l_('AUTHENTICATE_ACCOUNT_NOT_ACTIVATED'));
                 return false;
             }
             if ($user['blocked'] == 1) {
                 $session->setFlash('error', l_('AUTHENTICATE_ACCOUNT_BLOCKED'));
                 return false;
             }
             //account is found and can login, insert session data
             $user_session = array();
             $user_session['id'] = $user['id'];
             $user_session['name'] = $user['name'];
             $user_session['username'] = $user['username'];
             $user_session['email'] = $user['email'];
             $user_session['last_login'] = $user['last_visit'];
             $user_session['logged_in'] = 1;
             $user_session['groups'] = $user_groups;
             $user_session['inheritance'] = $user_inheritance;
             //get referer
             $referer = $session->get('_referer');
             $session->restart();
             $session->set('_referer', $referer);
             $session->set('user', array_merge($session->get('user', array()), $user_session));
             if (Base::getConfig('session_handler', 'php') == 'database') {
                 $session_model = new \GCore\Admin\Models\Session();
                 //$update = $session_model->updateAll(array('user_id' => $user['id'], 'site' => GCORE_SITE), array('session_id' => $session->get_id()));
                 $insert_status = $session_model->save(array('session_id' => $session->get_id(), 'user_id' => $user['id'], 'site' => GCORE_SITE, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'last_activity' => time()), array('new' => true));
             }
             //update last visit
             $user_model->updateAll(array('last_visit' => date('Y-m-d H:i:s', time())), array('id' => $user['id']), array('modified' => false));
             //after login hook
             $hook_results = Event::trigger('on_after_user_login');
             return true;
         } else {
             $session->setFlash('error', l_('AUTHENTICATE_INCORRECT_LOGIN_CREDENTIALS'));
             return false;
         }
     } else {
         return false;
     }
 }
Example #6
0
 function setFlash($type, $msg = '', $group = '')
 {
     $flashes = $this->get('__FLASH__', array());
     $path = array($type);
     if (!empty($group)) {
         $path = array($type, $group);
     }
     $type_msgs = Arr::getVal($flashes, $path, array());
     $type_msgs = array_merge($type_msgs, (array) $msg);
     return $this->set('__FLASH__', Arr::setVal($flashes, $path, $type_msgs));
 }
Example #7
0
 private function _save_hasMany($data)
 {
     if (!empty($this->hasMany)) {
         foreach ($this->hasMany as $alias => $model_info) {
             if (!empty($model_info['save_on_save']) and (bool) $model_info['save_on_save'] === true and isset($this->{$alias})) {
                 //if the model class is loaded, inject new join
                 $className =& $this->{$alias};
                 $foreignKey = $model_info['foreignKey'];
                 //check if this is a new record or an existing one
                 if ($this->created === true) {
                     //new record, update and save associated data
                     if (!empty($data) and !empty($data[$alias])) {
                         foreach ($data[$alias] as $k => $_d) {
                             $data[$alias][$k][$foreignKey] = $this->id;
                         }
                         $className->saveAll($data[$alias]);
                     }
                 } else {
                     //update existing associated record
                     if (!empty($data) and isset($data[$alias])) {
                         //delete non existent records based on p key values
                         if (!empty($model_info['delete_non_existent']) and (bool) $model_info['delete_non_existent'] === true) {
                             $existing_keys = Arr::getVal($data, array($alias, '[n]', $className->pkey));
                             $existing_keys = array_unique($existing_keys);
                             $existing_keys = array_filter($existing_keys);
                             $delete_conditions = empty($model_info['conditions']) ? array($foreignKey => $this->id, 'NOT' => array($className->pkey => $existing_keys)) : array_merge(array($foreignKey => $this->id, 'NOT' => array($className->pkey => $existing_keys)), $model_info['conditions']);
                             $className->deleteAll($delete_conditions);
                         }
                         if (!empty($data[$alias])) {
                             //fix any foreign key issues and save
                             foreach ($data[$alias] as $k => $_d) {
                                 $data[$alias][$k][$foreignKey] = $this->id;
                             }
                             $className->saveAll($data[$alias]);
                         }
                     }
                 }
             }
         }
     }
 }
 function addJsFile($path, $type = 'text/javascript')
 {
     if (substr($path, 0, 4) != 'http') {
         if (strpos($path, '/') === false) {
             $path = \GCore\Helpers\Assets::js($path);
         } else {
             //relative file path provided
             $path = $this->url . $path;
         }
     }
     if (!in_array($path, (array) Arr::getVal($this->jsfiles, array('[n]', 'src')))) {
         $this->jsfiles[] = array('src' => $path, 'type' => $type);
     }
 }
Example #9
0
 function _filter($columns = array())
 {
     $model_class = !empty($this->filter_model) ? $this->filter_model : null;
     if (empty($model_class)) {
         $alias = $this->get_main_model();
         if (!empty($alias)) {
             $model_class = $this->{$alias};
         } else {
             return;
         }
     }
     $prefix = '';
     if (!empty($this->filter_prefix)) {
         $prefix = '.' . $this->filter_prefix;
     }
     $session = Base::getSession();
     $filters = Request::data('fltr', $session->get(get_class($this) . $prefix . '.' . $model_class->alias . '.filters', array()));
     $conditions = array();
     if (!empty($filters)) {
         foreach ($columns as $k => $column) {
             $fv = Arr::getVal($filters, explode('.', $column), null);
             if (strlen($fv) > 0) {
                 $conditions[$column] = $fv;
             }
         }
     }
     Request::set('fltr', $filters);
     //set model conditions based on filters
     $model_class->conditions = array_merge($conditions, $model_class->conditions);
     $session->set(get_class($this) . $prefix . '.' . $model_class->alias . '.filters', $filters);
 }
Example #10
0
 function afterFind($type, &$data)
 {
     if (!empty($data) and is_array($data)) {
         $fresh_models = false;
         $_items = array_values($data);
         if ($data === $_items and !empty($_items[0][$this->alias])) {
             //this is a modeled data array
             $fresh_models = true;
             $this->_get_params_fields();
         }
         $models_list = array_merge(array($this->alias), array_keys($this->hasOne), array_keys($this->belongsTo));
         foreach ($data as $k => $record) {
             if ($fresh_models) {
                 $models_list = array_keys($record);
             }
             foreach ($models_list as $model) {
                 if (!empty($data[$k][$model])) {
                     if (isset($data[$k][$model]['extras'])) {
                         $extras = new Base64($data[$k][$model]['extras']);
                         $data[$k][$model]['extras'] = $extras->decode();
                     }
                     foreach ($this->params_fields as $params_field) {
                         if (isset($data[$k][$model][$params_field])) {
                             $params = new Parameter($data[$k][$model][$params_field]);
                             $data[$k][$model][$params_field] = $params->toArray();
                         }
                     }
                     if (!in_array($model, array('TagItem', 'Tag')) and !empty($this->hasMany['TagItem']) and isset($data[$k]['Tag'])) {
                         $tags = !is_null(Arr::getVal($data[$k]['Tag'], array('[n]', 'title'))) ? Arr::getVal($data[$k]['Tag'], array('[n]', 'title')) : array();
                         $data[$k][$model]['tags'] = implode(',', $tags);
                     }
                 }
             }
         }
     }
 }