Adding PHP Line Spaces In Codeigniter - php

I am trying to write file but just need to work out best way to make some gaps/spaces between some code “[‘default’][‘hostname’]” space . ‘=’ . space “‘localhost’”can not work it out.
At the moment when reload page it produces $db['default']['hostname']='localhost'; but need gap/space $db['default']['hostname'] = 'localhost';
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$output = '<?php' . "\n";
$output .= "\n";
$output .= '// DB' . "\n";
$output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";
$file = fopen(APPPATH . 'config/database-test.php', 'w');
fwrite($file, $output);
fclose($file);
$this->load->view('welcome_message');
}
}

$output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";
This line has syntax errors because you're missing the concat operator before the first space and after the second one (whatever the space is meant to be).
Instead of complicating things, why don't you just write this and avoid the concat hell:
$output .= '$db' . "['default']['hostname'] = 'localhost';\n";

I think I have found answer to make gap . " " . seems to do the trick.
$output .= '$db' . "['default']['hostname']" . " " . '=' . " " . "'localhost'". ";" . "\n";

As an additional note, I see you are writing a config file to be processed later.
I have found output buffering and var_export to do this job exceptionally well.
ob_start();
var_export($config);
$out = ob_get_clean();
then
fwrite($f, '$config = '.$out.';'); etc...
http://us3.php.net/manual/en/function.ob-get-clean.php
http://us3.php.net/manual/en/function.var-export.php
basically this will turn an array into a parse-able string such as
array(
'default'=>array(
'hostname'=>'localhost',
'user' => 'user', ///etc
)
)
then just add the variable part and the ending semi-colon
if you plan to do multiple values this would be a much cleaner approach

Related

Error validation the encoded script

I'm trying this
<?php
/* read the PHP source code */
$source_code = file_get_contents("hello.php");
$source_code = preg_replace('#^<\?php\s+#', '', $source_code);
$source_code = preg_replace('#\s+\?>\s*$#', '', $source_code);
/* create the encrypted version */
$redistributable_key = blenc_encrypt($source_code, "encrypt.php", "my_fixed_password");
$key_file = __DIR__ ."\keys";
file_put_contents($key_file, $redistributable_key . "\n", FILE_APPEND);
include 'encrypt.php';
echo $hello;
?>
hello.php
<?php
$hello = "Ciao";
I got this error
PHP Fatal error: blenc_compile: Validation of script
'encrypt.php' failed, cannot execute.
Please note that:
The key file is created, I'm already using the '\n' fix
I replaced <?php and ?> because another Stack Overflow question told me that it's a problem
<?php
$file_name = basename($file);
$unencrypted_key = = md5(time());
$source_code = file_get_contents($file);
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
?>
https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479
you should put the key to
blenckeys
file on your server
Note: Sometimes you need to reload the apache, if you have "Validation of script" issues
How to use BLENC in PHP?

Combining multiple php files

The current code provided on previous question is working. I need help to modify it so I can tell it what php files to combine.
The code below combines every .php file it finds in a directory.
(ie: Need to combine only the following pages, page1.php, page2.php, page3.php, page4.php, page5.php, page6.php, page7.php, page8.php, page9.php, page10.php, page11.php, page12.php )
Thanks in advance.
<?php
foreach (glob("*.php") as $filename) {
$code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code);
?>
If you know the file names and they are not going to change then you can do this:
<?php
$files = array("a.php", "b.php", "c.php");
$comb;
foreach ($files as $k)
{
$comb .= file_get_contents("./".$k);
}
file_put_contents("./combined.php",$comb);
?>
If you are getting them from a form submission then do something like this:
<?php
$files = array();
foreach ($_POST['files_to_combine'] as $k)
{
$files .= $k;
}
$comb;
foreach ($files as $k)
{
$comb .= file_get_contents("./".$k);
}
file_put_contents("./combined.php",$comb);
?>
** As a security note please make sure to sanitize your inputs if you use the second method! this code is only a proof of concept to make it simple to understand and use.
I have no idea why I get negative on my question asking to have it modified seeing how the programmer that answered it copied it from another site.
I don't do this for a living and have no plans too. These are personal
things I try to do to make my job go easier and without paper.
So here is the answer.
<?php
$txt1 = file_get_contents('page-001.php');
$txt1 .= "\n" . file_get_contents('page-002.php');
$txt1 .= "\n" . file_get_contents('page-003.php');
$txt1 .= "\n" . file_get_contents('page-004.php');
$txt1 .= "\n" . file_get_contents('page-005.php');
$txt1 .= "\n" . file_get_contents('page-006.php');
$txt1 .= "\n" . file_get_contents('page-007.php');
$txt1 .= "\n" . file_get_contents('page-008.php');
$txt1 .= "\n" . file_get_contents('page-009.php');
$txt1 .= "\n" . file_get_contents('page-010.php');
$txt1 .= "\n" . file_get_contents('page-011.php');
$txt1 .= "\n" . file_get_contents('page-012.php');
$fp = fopen('newcombined.php', 'w');
if(!$fp)
die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
die('Could not write to text file.');
echo 'Text files have been merged.';
?>

Back-up script and folder capitalisation

I have a site backup script that I am using, and it's working fine - apart from one issue. It does not respect and follow folder capitalisation, so "l" and "L" are compressed into one folder. Is there a way around that? The function is below.
Many thanks!
function BackupSite() {
$archname = './fullsite_' . date("Y-m-d-H-i-s") . '.tar.gz';
$command = 'tar -';
$command .= '-exclude ' . $archname . ' -czf ' . $archname . ' .';
$output = shell_exec($command);
$size = round(filesize($archname)/1000);

php shell_exec() interpolation

I have a php script that builds a dynamic command string (calls a perl script), then executes the command, like this:
$cmd_string = "perl $pushFile";
foreach($cmd_args AS $argName => $arg){
$cmd_string .= ' --' . $argName . '="' . $arg . '"';
}
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1');
I am getting failures that I think are being caused by interpolation of some of the arguments. For example is one of the arguments is '246+8GT>-', it gets turned into '246 8GT ' and an error that the string is unterminated. But, if I print_r $cmd_string to the screen and execute it via command line, or copy/paste it into the $cmd_string variable, it executes properly. I am stumped. How can I make sure these arguments are being passed properly? I tried this:
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . escapeshellcmd($cmd_string) . ' 2>&1');
but get the same result. Help?
You are escaping the comamand string, after it has been built.
Try this:
$cmd_string = "perl $pushFile";
foreach($cmd_args AS $argName => $arg){
$cmd_string .= ' --' . $argName . '="' . escapeshellarg($arg) . '"';
}
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1');

Reference to non-existent subpattern

I've made some progress with my regex that I'm using to extract attributes from pseudo-xml-tags, but then I got ambitous and wanted to correctly handle quoted attributes (with quotes being optional):
regex
~\{language\s*=\s*(P?<quote>[\"\']*)(?P<att>.*?)(?P=quote)\s*/\}~
(this is the output of the var that is used as arg in preg_match, so 'sensible things' such as \" were created with chr(92) . chr(34) beforehand...)
input
kjkjkjkjkjkj{language= 'DE' /}xxxxlxlxlxlllllk
extracts 'DE' when testing with RegexBuddy. But PHPs preg_match issues a warning: Warning: preg_match(): Compilation failed: reference to non-existent subpattern at offset 56.
What's the problem? I thought "quote" was assigned before...
Here's the complete program, just in case I have a PHP-error somewhere:
<?php
$QQ=chr(92) . chr(34);
$delimeters = "{}";
$del0 = preg_quote($delimeters{0});
$del1 = preg_quote($delimeters{1});
$tag="language";
$string="fdfdfdfdf{language=1}testhgg";
$preg1 = "|" . $del0 . $tag . "[^" . $del1 . "]*" . $del1 . "(.*?)" . $del0 . "/" . $tag . $del1 . "|";
$preg2 = "~" . $del0 . $tag . "\s*=\s*(?P<" . "quote>[" . $QQ . "\']*)(?P<att>.*?)(?P=quote)\s*/" . $del1 . "~";
$match=array();
preg_match($preg1,$string,$match);
echo "<br>match1:<pre>";var_dump($match);echo"</pre>";
$match=array();
preg_match($preg2,$string,$match);
echo "<br>match2:<pre>";var_dump($match);echo"</pre>";
?>
Your named subpattern is formatted incorrectly.
(P?<quote>[\"\']*)
should be
(?P<quote>[\"\']*)
See http://php.net/manual/en/regexp.reference.subpatterns.php

Categories