Codeigniter handling lang files? - codeigniter

I know how to use CI lang helper and lang class, but my question is it possible to
use lang files from other directory than SYSTEM, maybe i can make a new folder in root of app and call language files from there?
And other thing, is is good handling of lang files that CI done?
I want to add somthing that user can uplaod lang files, but structure need to be checked, here
is an example
$lang["msg_first_name"] = "First Name";
This is proper lang line, but what if some upload file that looks like this?
$lang["msg_first_name"] = "First Name i "Michael" and i like chocolate";
You see there is two
"
Does that will create error in lang file, or CI handle that, i or must made a custom class
that will handle that error?

Why the your language in the stystem folder it should be inside the application/language folder. And its not really a good idea to upload the language by user. Its a big SECURITY HOLE because any one can upload any script and that will be executed by your code. You never should allow to upload any executable code in your system.
And if you upload a broken file it will cause the php error. So when there is a php error codeigniter cannot do anything about this. Just simply think you are including a php file as normally you would do for any php. So CI doing the same then reading some variable($lang) and assigning to the a class nothing else.
And yes you can use your define folder to load the language you can do this in few way
$this->load->add_package_path('YOUR_PATH');
//so your language will be YOUR_PATH/language/YOUR_LANG_FOLDER(english default)
$this->load->language('language_file'); //user
OR
$this->lang->load('YOUR_LANG_FILE','LANG',FALSE,TRUE,'YOUR_PATH') ;
//look the function description
/**
* Load a language file
*
* #access public
* #param mixed the name of the language file to be loaded. Can be an array
* #param string the language (english, etc.)
* #param bool return loaded array of translations
* #param bool add suffix to $langfile
* #param string alternative path to look for language file
* #return mixed
*/
OR extend the CI_Lang and modify the load function they way you want, to do this you have to create a file `application/core/MY_Lang.php`
class MY_Lang extends CI_Lang {
//OVERWRITE THE FUNCTION
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = ''){
//MODIFY THE THE CODE FOMR CI_Lang AND SET THE INCLUDE PATH AS YOU WANT
}
}

You may write different language files and load them whenever you want. Read the following link: http://www.sitepoint.com/multi-language-support-in-codeigniter/
The folder you put your files is application/language/[LANG], and then you just call them in your controller, no need to any further modification. It's important note that the files MUST finish with "_lang.php"
About uploading the file, just deploy your language file (must have the same name in the different languages folders) into the folder and just go. What I've done with similar multilanguages files is building a controller which I use to import the languages lines from a CSV files and write the different lines into the language files:
Code | Message
name | Su nombre
surname | Su apellido
With that, you write the whole language file from there, checking for weird symbols. You may even have several columns with different languages, and keep the coherence with it because you overwrite the file every time you launch the importer_language:
Code | MsgSpanish | MsgEnglish
name | Su nombre | Your Name
surname | Su apellido | Your Surname

Related

Generate files with laravel command

I need to create a number of files in a specific format. So I planned to create a generate command by extending GeneratorCommand. I want to create view files and view-config files in application root directory.
The problem is, I did not find any official doc to do so. There are some article in the web, which suggests to use getDefaultNamespace method to set the path like the following. I was following the steps suggested at https://laravelpackage.com/06-artisan-commands.html#creating-a-generator-command. But I want to create files in root dir not into app dir. When i remove the $rootNamespace form the method it does not create files.
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Actions';
}
how can i create a command to generate files in specific directories in the application?
The class GeneratorCommand has a protected method rootNamespace. If I understand correctly, it returns the root ouf your application.
So you should be able to override the method getNamespace like so:
/**
* Get the full namespace for a given class, without the class name.
*
* #param string $name
* #return string
*/
protected function getNamespace($name)
{
$rootNamespace = trim($this->rootNamespace(), '\\');
return $this->getDefaultNamespace($rootNamespace);
}
NOTE
You can see a working example in one of my open source projects.

Theme is caching previous user name

We are using CAS to login to our Drupal instance. This is working correctly and displaying the correct user content (blocks etc. based on roles). What is not working correctly is the small snippet in the theme that says welcome . It keeps showing the previous user who logged in.
How do I set this in bigpipe?
The code looks like this in the theme: <span id="user_name">{{user.displayname}}</span>
Is there a way to tell bigpipe not to cache this?
This code snippet is on one of our twig files header.twig.html which is a partial.
I ended up putting this in a block, and just referencing the block section in the theme instead of just pulling that, then I used the block to be ignored for caching.
Thanks!
I used this post with other resources to solve a similar problem. We were including {{ user.displayname }} in a twig template for the header on all pages of our site. Some users were seeing other users' names in the header after signing in. We wanted to solve the problem while impacting caching as little as possible. Here, I share in detail what we did in the hope that it will help others. I'll use the specific names used in our code. Readers will need to adjust to their own names. (The code follows our prescribed format, so please forgive that it isn't standard.)
Step 1
Create a custom module. Custom module creation is covered adequately in other places, so I won't give details here. Our custom module is named rsc.
Step 2
Create the folder modules/custom/rsc/src/Plugin/Block and in it create a file named DisplayName.php.
Step 3
In the file DisplayName.php, include the following:
<?php
namespace Drupal\rsc\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\user\Entity\User;
/**
* A block to show the user's display name
*
* #Block(
* id = "display_name",
* admin_label = "Display Name"
* )
*/
class DisplayName extends BlockBase // The class name must match the filename
{
public function build()
{
$user = User::load(\Drupal::currentUser()->id());
return
[
'#type' => 'markup',
'#markup' => $user->getDisplayName(),
'#cache' => ['contexts' => ['user']]
];
}
}
Step 4
Clear the Drupal cache to make the new block available at Block Layout.
Step 5
Go to Admin > Structure > Block Layout and place the Display Name block in the Hidden Blocks For Referencing - Not Displayed section (bottom of the list on our site). In the Configure Block dialog, edit the Machine name to display_name to match id in the code above. Clear the Display title checkbox and save. Doing this makes the block available for use in twig templates.
Step 6
In the twig template for the header, replace
{{ user.displayname }}
with
{{ drupal_entity('block', 'display_name') }}
The drupal_entity function is part of the twig_tweak module, which we were already using. It allows insertion of a custom block into a twig template. If you're not using this module, you'll need to install it, or find another method of including a block in a twig template.
Step 7
Clear the Drupal cache to make the modified template take effect.
If you see anything about this that can be improved, please comment.

Laravel: How to prevent users loading files from outside the base path

I have a Laravel website and I have several routes that load the contents of images from Storage. I do this using the following code:
public function show_image($name) {
echo Storage::disk('images')->get($name);
}
I want to prevent users being able to set name to something like ../../../error.log. So I don't want users to escape the Storage directory. I have a few ideas on how to accomplish this however I want to know is there a best practice?
If you need just file name, not location, disallow them from inputting folder of any kind. Just cut the string on /.
end(preg_split("#/#", $name));
When you need to allow some folders and all of the contents, check the folder name, subfolder name, etc.
You could either keep a registry/index of the uploaded images, and only allow the user to show a image from that registry (e.g. an images database table).
Or you could do a scan of the directory, that you are allowing files from, and make sure, that the requested file is in that list.
public function show_image($name) {
$files = Storage::disk('images')->files();
if (! in_array($name, $files)) {
throw new \Exception('Requested file not found');
}
echo Storage::disk('images')->get($name);
}
(code untested)

Move or copy a file from the request to multiple locations

I'm using Laravel and taking in input and file uploads. This page takes changes that users want to make to an order. The end goal is to take this input and apply it to multiple orders.
I can reuse the input for each of the orders. But what would be a good way for me to copy that file in the request to multiple directories? For example, as in the documentation, I originally have: $request->file('photo')->move($destinationPath, $fileName); which works fine for one order. But now that I want to apply it to many orders, I can't move that same file from the request anymore since it doesn't exist.
And it looks like the UploadedFile object doesn't have a copy() method. Any ideas would be appreciated. Thank you.
Do not depend on component too much. Keep it simple
$request->file('photo')->move($destination_path, $file_name);
//Add DIRECTORY_SEPARATOR between path and filename if needed
copy($destination_path.$file_name, $new_path.$new_file_name);
If I may make a suggestion assuming these files are going to stay the same (you aren't allowing your users to modify these files by order), I think it makes sense to store one copy of the file on the server and use a database table to determine which orders that file belongs to and manage it all in the table. You may need to create a files table and an file_order table and give it a many to many relationship to your orders table but in the end, if these files are allowed to be large, could save you a lot of space and in my opinion, make it easier to manage.
In Laravel, look at the vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php , and I hope you'll get the everything of file, how they manage the files.
How to copy a file, move a file, delete a file everything is described clearly in that.
For copy a file in Laravel, there function is:
/**
* Copy a file to a new location.
*
* #param string $path
* #param string $target
* #return bool
*/
public function copy($path, $target)
{
return copy($path, $target);
}
I think there's nothing to say now.
Just use that
File::copy('file_name_with_full_path', 'target_directory_where_copy');
Hope, it might be helpful for you.

What for is the direct_front_name tag in Magento

Some modules have a <request><direct_front_name>...</direct_front_name></request> in their module config, for example xmlconnect and api. What is this tag for?
What I think it is for:
xmlconnect and api are both used as direct entry points for the site (as opposed normal modules which are reached mostly from within the site). So in combination with the option to use store codes in your store urls you can specify a direct_front_end tag to make the store code not necessary for those modules. This way there is no 404 when calling them without a store code.
(Kind of answered it myself, but couldn't find any information about it online. Might be of use to others. And maybe anyone has something to add.)
You're totally right. And the php DOC clearly tells so :
Mage_Core_Controller_Request_Http::isDirectAccessFrontendName() :
/**
* Check if code declared as direct access frontend name
* this mean what this url can be used without store code
*
* #param string $code
* #return bool
*/
public function isDirectAccessFrontendName($code)

Resources