I am using codeigniter framework for my site, but in form_validation I am getting error I followed this link in stackoverflow but it didn't work for me
followed link:
idn_to_ascii() in 5.2.17
Issue:
code in codeigniter libraries/form_validation.php:
public function valid_email($str)
{
if (function_exists('idn_to_ascii') && preg_match('#\A([^#]+)#(.+)\z#', $str, $matches))
{
$domain = defined('INTL_IDNA_VARIANT_UTS46')
? idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46)
: idn_to_ascii($matches[2]);
if ($domain !== FALSE)
{
$str = $matches[1].'#'.$domain;
}
}
return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
}
The ideal solution would be to upgrade ICU to its latest version
As this was not possible at my shared server, I resolved that problem, extending the CI email library:
overrules valid_email() function which uses INTL_IDNA_VARIANT_UTS46, which is unfortunately not installed on my server.
PhP 7.2 works with that version, so if you have INTL_IDNA_VARIANT_2003 installed, you get the above deprecated error message.
SOLUTION: you need to go back to valid_email() function from 2.0 version email library:
class MY_Email extends CI_Email {
public function valid_email($address)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
}
}
Save this extended Class as MY_email.php in your application/libraries folder. About Extending Native Libraries, the prefix MY_ is configurable.
Related
Why does Codeignitor not accept Controller in composer autoload when validating routes?
It's checking by: class_exists($class, FALSE) where the second parameter disables checking in autoload.
https://github.com/bcit-ci/CodeIgniter
$e404 = FALSE;
$class = ucfirst($RTR->class);
$method = $RTR->method;
if (empty($class) OR ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php'))
{
$e404 = TRUE;
}
else
{
require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php');
if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
{
$e404 = TRUE;
}
elseif (method_exists($class, '_remap'))
{
$params = array($method, array_slice($URI->rsegments, 2));
$method = '_remap';
}
elseif ( ! method_exists($class, $method))
{
$e404 = TRUE;
}
/**
* DO NOT CHANGE THIS, NOTHING ELSE WORKS!
*
* - method_exists() returns true for non-public methods, which passes the previous elseif
* - is_callable() returns false for PHP 4-style constructors, even if there's a __construct()
* - method_exists($class, '__construct') won't work because CI_Controller::__construct() is inherited
* - People will only complain if this doesn't work, even though it is documented that it shouldn't.
*
* ReflectionMethod::isConstructor() is the ONLY reliable check,
* knowing which method will be executed as a constructor.
*/
elseif ( ! is_callable(array($class, $method)))
{
$reflection = new ReflectionMethod($class, $method);
if ( ! $reflection->isPublic() OR $reflection->isConstructor())
{
$e404 = TRUE;
}
}
}
Looking over the git history, the change was introduced in 49e68de96b420a444c826995746a5f09470e76d9, with the commit message being:
Disable autoloader call from class_exists() occurences to improve performance
Note: The Driver libary tests seem to depend on that, so one occurence in CI_Loader is left until we resolve that.
So the nominal reason is performance.
If you want to ensure that the controller classes will be loaded on each request, you can add the files explicitly to the Composer autoload.files attribute, like so:
composer.json
{
"autoload": {
"files": [
"src/Foo.php"
]
},
"name": "test/64166739"
}
src/Foo.php
<?php
class Foo {}
test.php
<?php
$loader = require('./vendor/autoload.php');
var_dump(class_exists('Foo', false));
When run (via php test.php for example), we get the following output:
bool(true)
Additional
Looking over the code around that call to class_exists, it would appear that the controller files should follow a convention such that, for example with the built in Welcome controller and the default settings, the file that defines it should exist at:
application/controllers/Welcome.php
and so after require_onceing that file, the call to class_exists is a reasonably simple sanity check to ensure that the file did in fact define that class. So, based on this assumption about how controllers are added to the CodeIgniter application (ie all in the application/controllers directory and named the same as the class that they define), it's reasonable to bypass the autoloader when performing that check.
If you wanted to ensure the controllers are loaded when needed, the CodeIgniter way, they should be added to the application as listed above.
Some of you might think that this question was already asked hundred of times, still I haven't found an answer to the specific problem I have.
I have read these topics but none of them answers the question I have:
https://github.com/bcit-ci/CodeIgniter/issues/1805
codeigniter upload not working on linux
Codeigniter web app is not working with the linux but here on windows is fine
Naming convention issues when using codeigniter in windows and linux
I am developing a PHP application. My test server at home runs Windows, the stage server runs Ubuntu (Amazon AWS).
My application worked flawlessly until I needed to create a helper:
MY_form_helper.php
In that helper I needed to overwrite this:
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if ( ! isset($_POST[$field]))
{
return $default;
}
return form_prep($_POST[$field], $field);
}
return form_prep($OBJ->set_value($field, $default), $field);
}
with this:
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if (isset($_POST[$field]))
{
return form_prep($_POST[$field], $field);
}
if (isset($_GET[$field]))
{
return form_prep($_GET[$field], $field);
}
return $default;
}
return form_prep($OBJ->set_value($field, $default), $field);
}
So I saved the changes to the
MY_form_helper.php
Tested it all on windows and great everything works. All you PHP gurus probably know what happens now, I upload to linux server and errors :)
First error I get this:
An Error Was Encountered
Unable to load the requested file: helpers/my_form_helper.php
Well ok, linux is case sensitive so I have changed all the upper case file names to the lower case ones. After doing that I was greeted by blank page.
Side note: I have libraries that have capitalised first letter in name (for example I use flexi_auth library) and it always worked on the server. I dont know why now, as surely it should have failed on the Linux because of the capital letters in file names - would be great if someone could explain.
I have changed config in my index.php to display all the errors by adding
ini_set('display_errors', 1);
here:
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
error_reporting(E_ALL);
ini_set('display_errors', 1);
break;
case 'testing':
case 'production':
error_reporting(0);
break;
default:
exit('The application environment is not set correctly.');
}
}
I reload the page and now I can see that there is fatal error:
Fatal error: Cannot redeclare set_value() (previously declared in /var/www/html/stage/bik/system/helpers/form_helper.php:675) in /var/www/html/stage/bik/application/helpers/my_form_helper.php on line 5
And here is where I am stuck. I did a big no-no and edited the form_helper in the core system files of CI with my code. I needed it all to work as I have people testing the whole thing and I couldn't have them wait.
So could someone explain to me what is going on? I am relatively new to Linux having always used Windows machines and servers. But I want to go with the flow and use Linux as cool kids do.
Here are few more bits that might help
MY_form_helper file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if (isset($_POST[$field]))
{
return form_prep($_POST[$field], $field);
}
if (isset($_GET[$field]))
{
return form_prep($_GET[$field], $field);
}
return $default;
}
return form_prep($OBJ->set_value($field, $default), $field);
}
This is how I load it in controller:
$this->load->helper('MY_form_helper');
I have changed both capital letter to lower case when moving it to linux server.
And now something that boggles my mind, I have created MY_Input library and I am loading it from core folder in CI Appliaction folder. This thing has upper case letters in name and it works on the Linux server.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Input extends CI_Input {
function save_query($query_array){
$CI =& get_instance();
$CI->db->insert('ci_query', array('query_string' => http_build_query($query_array)));
return $CI->db->insert_id();
}
function load_query($query_id){
$CI =& get_instance();
$rows = $CI->db->get_where('ci_query', array('queryid' => $query_id))->result();
if (isset($rows[0])) {
parse_str($rows[0]->query_string, $_GET);
}
}
}
I am still learning PHP and I do not get many things and I do not have much experience so I really appreciate all the help anyone can give me.
I hope I made clear what I need answering. Thank you to whomever who is willing to share their knowledge with me!
I think you can wrap the function into the class
class MY_form_helper{
static function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if (isset($_POST[$field]))
{
return form_prep($_POST[$field], $field);
}
if (isset($_GET[$field]))
{
return form_prep($_GET[$field], $field);
}
return $default;
}
return form_prep($OBJ->set_value($field, $default), $field);
}
}
and you will be able to call it like:
MY_form_helper::set_value($field, $default);
or you need to use namespaces but this is longer
Could someone explain me how does Codeigniters handle the load of a library previously loaded?
Are the library loaded once again?
Does it simply jumps into the next function call?
This is something that might happen accidentally. Recently, while working on a project, I created an hook for post_controller_constructor and inside of it i start loading a class to enhance the functionality of my website.
With the hook on place i forgot to remove the old load library call from my controllers.
Curiously, nothing of wrong happened.
I was expecting an exception telling me that the library was already loaded or something like that.
CI checks if library is not set.
As you can see in this code :
public function library($library = '', $params = NULL, $object_name = NULL)
{
if (is_array($library))
{
foreach ($library as $class)
{
$this->library($class, $params);
}
return;
}
if ($library == '' OR isset($this->_base_classes[$library]))
{
return FALSE;
}
if ( ! is_null($params) && ! is_array($params))
{
$params = NULL;
}
$this->_ci_load_class($library, $params, $object_name);
}
system\code\Loader.php
i just installed a new component in my site but when i click on component settings button in the backend i am getting this message:
Fatal error: Call to a member function loadByOption() on a non-object in /mysite.com/administrator/components/com_sigpro/models/settings.php on line 32
the file contains this code:
defined('_JEXEC') or die ;
class SigProModelSettings extends SigProModel
{
protected $extensionID = null;
public function getForm()
{
$option = $this->getState('option');
if (version_compare(JVERSION, '2.5.0', 'ge'))
{
$component = JComponentHelper::getComponent($option);
$this->extensionID = $component->id;
JForm::addFormPath(JPATH_ADMINISTRATOR.'/components/'.$option);
$form = JForm::getInstance($option.'.settings', 'config', array('control' => 'jform'), false, '/config');
$form->bind($component->params);
}
else
{
$component = JTable::getInstance('component');
$component->loadByOption($option);
$this->extensionID = $component->id;
$form = new JParameter($component->params, JPATH_ADMINISTRATOR.DS.'components'.DS.$option.DS.'config.xml');
}
return $form;
}
PS my site is running Joomla 1.7.5 stable version and i cant upgrade it to 2.5 because the site is online and productive some components dont works on version 2.5 .
So i will appreciate it if someone can help me fix this error.
Thanks in advance
you 1.6 needs 1.6 specific modules, components & plugins. You'll need to find a 1.6 compatible version or alternative or check the permissions on /configuration.php
Looks like the table class is not present in your file system. It should have been there in the /mysite.com/administrator/components/com_sigpro/tables folder , somewhere ( may vary depending on the code, but looks like they have not included a custom path ). The class should extends JTable class.
I need some help to understand CodeIgniter's hook logic to adapt the code to my needs.
The page : https://www.codeigniter.com/user_guide/general/hooks.html
In fact, I had to modify the database driver for MySQL from this :
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';
}
to this :
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return implode(', ', $tables);
}
I made this mod to use UNION queries using Active Record library.
Can someone help me to make a hook in order to prevent my modification from being overwritten when I update the core system ?
Thanks in advance !
You can find the instructions for extending the db drivers on the CodeIgniter Wiki - Extending Database Drivers
The solution comes in 3 simple steps:
1) Extend your loader class by creating the file MY_Loader.php. Put it
into your libraries directory in the application path (or if you are
using CI 2.x.x then put it into application\core\ path):
2) Add the following function to your MY_Loader class:
3) Create your Database driver extension class, that you name
MY_DB_mysql_driver.php (or substitute the mysql part for whatever
driver you use - do that also for the classnames in the code below!).
Put this file also in your applications libraries directory:
Your custom DB driver will look like this
class MY_DB_mysql_driver extends CI_DB_mysql_driver {
function __construct($params){
parent::__construct($params);
log_message('debug', 'Extended DB driver class instantiated!');
}
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return implode(', ', $tables);
}
}