Codeigniter can't load big custom library - php

An Error Was Encountered Unable to load the requested class:
Plugin_update_checker
I am using custom library which create a dynamic php file with 740 line code
controller function:
public function create() {
$userId=$this->session->userdata('userId');
$userName=$this->session->userdata('userName');
//Form_validation stuff
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('requires', 'Requires', 'required');
$this->form_validation->set_rules('url', 'Homepage URL', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}else {
$data = array(
'title' => $this->input->post('title'),
'readme_text' => ($this->input->post('readme_text') == 'yes' ? 1 : 0),
'description' => $this->input->post('description'),
'requires' => $this->input->post('requires'),
'url' => prep_url($this->input->post('url')),
'user_id' => $userId
);
$insert_id = $this->model->insert_data($data);
print ($insert_id);
if ($insert_id) {
$this->session->set_userdata('pluginId', $insert_id);
$key = substr(number_format(time() * rand(),0,'',''),0,6).$insert_id;
//$this->model->update_data(array('plugin_id' => $insert_id), array('secret_key' => $key,'php_file' => $WinnRepoPluginUpdater));
$this->load->library('RepositoryHelper');
// ------------------------Can't Load this Library ---------------------
$this->load->library('Plugin_update_checker');
// --------------------------------------------------------------------
$repo= new RepositoryHelper();
$php_code = new plugin_update_checker();
$dir = 'api/'.$repo->sum($userId,1548).'-'.$repo->slug($userName).'/plugins/';
$file_name = 'WinnRepo'.$repo->slug_space($data['title']).'PluginUpdater_'.$key;
$WinnRepoPluginUpdater = $dir.'/'.$file_name.'.php';
$repo->make_directory($dir);
$url = base_url().'home/plugin_version_control/';
//$code = $php_code->plugin_update_checker('WinnRepo_'.$key, $file_name, 'WinnRepoPluginUpdateChecker_2_1', $url, $key);
$repo->make_file($WinnRepoPluginUpdater,'ccccccccccc');
$this->model->update_data(array('plugin_id' => $insert_id), array('secret_key' => $key,'php_file' => $WinnRepoPluginUpdater));
redirect('Plugins/view');
}
}
}
Here is custom Library:
class Plugin_update_checker
{
public function plugin_update_checker($winnRepo, $className, $PluginUpdateChecker_2_1, $url, $licence_key)
{
echo '<?php ';
?>
// Here 740 line which is auto generated a php file
<?php
}
}

Library name: /application/libraries/Plugin_update_checker.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Plugin_update_checker
{
function __construct()
{
$this->ci =& get_instance();
}
function test(){
return 'test';
}
}
Controller Code:
public function index()
{
$this->load->library('Plugin_update_checker');
echo $this->plugin_update_checker->test();
}
Try this! This code work for me.

Related

Use form_validation correctly in CodeIgniter

I have a registration system with CodeIgniter but currently I have no control on email and password. A user can register without putting email or a password.
I have an index() and a register_user function() in my Signup controller but the redirection is not working on success
At the moment I have the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->load->view('home-view');
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
function register_user(){
$this->load->library('custom_u_id');
$data = array('user_id' => $this->custom_u_id->construct_id('USR'),
'name' => $_POST['name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->load->model('signup_model');
$user_details = $this->signup_model->register_user($data);
if (!empty($user_details)){
$user_data = array
(
'user_id' => $user_details['user_id'],
'email' => $user_details['email'],
'name' => $user_details['name'],
'user_type' => $user_details['user_type'],
);
$this->session->set_userdata('sessiondata',$user_data);
if (intval($user_details['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
} else{
redirect('login');
}
}// end of function login
}
Do I need to put the form_validation in my register_user function ? I've tried but the check doesn't work anymore...
I also have in my view the <?php validation_errors();?> function and the <?php form_open(base_url().'signup');?>
looking by your code, i think you want to put register_user() inside validation TRUE since the query is in that method.
so try to change your code to this :
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->register_user();
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
and be sure your form action to like this :
<form action="<?=site_url('/signup/index');?>">

How to send post values in RESTful API using CodeIgniter?

I don't have any idea how to get values please help me to sort this problem. Tell me with a reference if someone already has the code then please share. I'm also curious as how to load spark with cURL with a RESTful API with full procedure
<?php class LoginController extends CI_Controller {
public function index()
{
$this->load->view('admin/header');
$this->load->view('admin/index');
$this->load->view('admin/footer');
}
public function loginCon(){
$this->load->Library('rest');
$this->form_validation->set_rules('email', 'E-mail', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");
if ($this->form_validation->run()==false)
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->session->set_flashdata('login_failed', 'Invalid User Name Password');
}else{
$config = array('server' => "http://api.amid.tech/hsApiV2/api/demo.php/",
'http_user' => 'admin',
'http_pass' => 'xxxxx',
'http_auth' => 'basic',
);
$this->rest->initialize($config);
$method = 'post';
$param = array(
'UserEmail' => $this->input->post('email'), // works fine here
'UserPass' => $this->input->post('password'),
'UserRoleId'=>1
);
$uri = 'adminlogin';
$this->rest->format('application/json');
$result = $this->rest->{$method}($uri, $param);
echo $result;
$this->load->view('admin/admin_header');
$this->load->view('admin/sidebar');
$this->load->view("admin/dashboard");
$this->load->view('admin/dashboard.php');
}
}
public function registerd()
{
$this->load->view('admin/header');
$this->load->view('admin/registration');
$this->load->view('admin/footer');
}
} ?>
To get raw inputs try
// get the raw POST data
$rawData = file_get_contents("php://input");
For validation try
$this->form_validation->set_rules($rawData['email'], 'E-mail', 'required|trim');

converting submitted form data in codeigniter using inflector helper

I have a codeigniter form which runs some basic validation and submits data to the database. But I want to additionally alter the post data of one of the fields to use the inflector helper in order to convert the posted data to camel case before submitting to the database. How do I do this?
Here is my current form:
<?php echo form_open('instances/create') ?>
<label for="content">Content</label>
<textarea name="content"></textarea><br />
<input type="submit" name="submit" value="Create" />
</form>
Here is my current controller:
public function create(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('inflector');
$data['title'] = 'Create an instance';
$this->form_validation->set_rules('title', 'Title', 'required');
//want to camelize the 'title' here
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('instances/create');
$this->load->view('templates/footer');
}
else
{
$this->instances_model->set_instances();
$this->load->view('instances/success');
}
}
and here's my model:
<?php
class Instances_model extends CI_Model {
public function __construct(){
$this->load->database();
}
public function get_instances($slug = FALSE){
if ($slug === FALSE){
$query = $this->db->get('extra_instances');
return $query->result_array();
}
$query = $this->db->get_where('extra_instances', array('slug' => $slug));
return $query->row_array();
}
public function set_instances(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'slug' => $slug,
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'year' => $this->input->post('year'),
'credit' => $this->input->post('credit'),
'source' => $this->input->post('source')
);
return $this->db->insert('extra_instances', $data);
}
}
I know that you can camelize a variable with the following:
echo camelize('my_dog_spot'); // Prints 'myDogSpot'
and I know that you can run custom validation like this:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
But I'm lacking the knowledge of how to put this altogether to quickly change the POST data before submitting to the database.
Nothing too complicated, you can do it after you pass the validation, just before inserting your data array into the database:
$data = array(
'slug' => $slug,
'title' => camelize($this->input->post('title')),
// ...

CodeIgniter 3 - Callable Form Validation by Config file not working

I am unable to get the callable form validation feature of CodeIgniter 3 to work when the validation rules are placed in a separate config file.
I am getting the following error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Config::$form_validation_callback_library
The config file with the form validation rules are as follows (config/fvalidation.php):
$config['client_details'] = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array($this->form_validation_callback_library, 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid ABN has been entered %s.')
)
);
The form validation class attempting to be called is (i.e. $this->form_validation_callback_library):
class Form_validation_callback_library
{
public function abn_check()
{
$this->load->library('abn_validator');
$abn = $this->input->post_get('abn', TRUE);
if (!$this->abn_validator->isValidAbn($abn)) {
return FALSE;
}
return TRUE;
}
}
The controller is:
$this->config->load('fvalidation');
$validation_rules = $this->config->item('client_details');
$this->form_validation->set_rules($validation_rules);
if ($this->form_validation->run() == FALSE) {
// show form
} else {
// process form data
}
Any help would be greatly appreciated.
Cheers,
VeeDee
I would use codeigniter callback example below callback
http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods
<?php
class Example extends CI_Controller {
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('client_abn', 'ABN Number', 'required|callback_checkabn');
if ($this->form_validation->run() == FALSE) {
$this->load->view('something');
} else {
// Redirect to success page i.e login or dashboard or what ever
redirect('/'); // Currently would redirect to home '/'
}
}
public function checkabn() {
$this->load->library('abn_validator');
$abn = $this->input->post('abn');
if (!$this->abn_validator->isValidAbn($abn)) {
$this->form_validation->set_message('checkabn', 'Invalid ABN has been entered %s.');
return FALSE;
} else {
return TRUE;
}
}
}
And on your view in or above form add
<?php echo validation_errors('<div class="error">', '</div>'); ?>
<form action="<?php echo base_url('example');?>" method="post">
<input type="text" name="client_abn" placeholder="" value="" />
</form>
This is a most common problem we face when we run custom form validation in CI. Whether the callback function is in the same controller or it is in the a library of callback function we need to pass the accessible object of the class containing the callback function.
So when your run the
$callable_validations = new Form_validation_callback_library();
$this->form_validation->run($callable_validations)
Looks like this is not possible currently on CodeIgniter 3.
I have created a crude workaround.. so please go ahead an improve it because it doesn't look pretty :)
Update the config file like so (/config/fvalidation.php):
$config['client_details'] = = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid %s has been entered .')
)
);
Note the following line in the config file above as we will be using them as flags in the controller code:
array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))
The Library is pretty much the same except we load the instance (/libraries/Form_validation_callback_library.php):
class Form_validation_callback_library
{
private $_CI;
function Form_validation_callback_library() {
$this->_CI =& get_instance();
log_message('info', "Form_validation_callback_library Library Initialized");
}
public function abn_check($abn)
{
$this->_CI->load->library('abn_validator');
if (!$this->_CI->abn_validator->isValidAbn($abn)) {
return FALSE;
}
return TRUE;
}
}
In the controller we load the library (/controllers/Foo.php):
// load the config file and store
$this->config->load('fvalidation', TRUE);
$rule_dataset = $this->config->item('client_details', 'fvalidation');
// search and load the 'callable' library
foreach ($rule_dataset as $i => $rules) {
if (isset($rules['rules'])) {
foreach ($rules['rules'] as $k => $rule) {
if (is_array($rule) && preg_match("/_callable/",$rule[0]) && isset($rule[1][0])) {
list ($load_type, $load_name) = explode(":", $rule[1][0]);
// load the library
$this->load->$load_type($load_name);
$rule_dataset[$i]['rules'][$k][1][0] = $this->$load_name;
}
}
}
}
// set the rules
$this->form_validation->set_rules($rule_dataset);
// load the form
if ($this->form_validation->run() == FALSE) {
// show form
} else {
// process form data
}
I did something similar to Vidura, but extended the Form Validation library by adding MY_Form_validation.php with the following code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class GS_Form_validation extends CI_Form_validation {
public function set_rules($field, $label = '', $rules = array(), $errors = array())
{
if (is_array($rules))
{
foreach ($rules as &$rule)
{
if (is_array($rule))
{
if (is_array($rule[1]) and is_string($rule[1][0]))
{
// handles rule like ['password_check', ['library:passwords', 'check_valid_password']]
// You would set_message like $this->form_validation->set_message('password_check', 'Incorrect password');
// The advantage of defining the rule like this is you can override the callback functions error message
list ($load_type, $load_name) = explode(":", $rule[1][0]);
$CI =& get_instance();
$CI->load->$load_type($load_name);
$rule[1][0] = $CI->$load_name;
}
else if (is_string($rule[0]))
{
// handles rule like ['library:password', 'check_valid_password']
// You would set_message like $this->form_validation->set_message('check_valid_password', 'Incorrect password');
list ($load_type, $load_name) = explode(":", $rule[0]);
$CI =& get_instance();
$CI->load->$load_type($load_name);
$rule[0] = $rule[1];
$rule[1] = [$CI->$load_name, $rule[1]];
}
}
}
}
return parent::set_rules($field, $label, $rules, $errors);
}
}
Then you can define callbacks to library functions like:
$this->form_validation->set_rules(['library:passwords', 'check_valid_password']);
Where passwords is the library and check_valid_password is the method.
I've simply do (config/form_validation.php):
$CI =& get_instance();
$CI->load->model('form_validation_callback_library');
$config['client_details'] = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array($CI->form_validation_callback_library, 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid ABN has been entered %s.')
)
And it works to me...
I'm running on Codeigniter 3.0.4

creating news item in codeigniter

i want to create news item in codeigniter with use slug but is error display like this.
> Call to a member function insert() on a non-object in line 34
I am a newbie in Codeigniter and couldn't really figure out how to solve this.
my controller
function tambah_profil()
{
$this->data['title'] ='create a new items';
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);
}else{
$this->mhalaman->insert_profil();
$this->data['contents'] = $this->load->view('admin/profil/view_profil', '', true);
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);
//$this->data['contents'] = 'admin/profil/tambah_profil';
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
my model
var $db;
private $tbl_halaman = 'halaman';
public function __construct()
{
parent ::__construct();
$this->load->database();
}
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
function insert_profil()
{
$slug = url_title($this->input->post('judul'),'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'slug' => $slug,
'content' => $this->input->post('content')
);
return $this->db->insert($this->tbl_halaman ,$data); //line 34
}
please help me what to do. thank you.
Please check whether you have loaded $this->load->database() inside the model constructor.
$this->db->method_name(); will only work when the database library is loaded.
If you plan to use the database throughout your application, I would suggest adding it to your autoload.php in /application/config/.
$autoload['libraries']=array('database');

Categories