drqefnd544707688 2017-03-10 10:10
浏览 62
已采纳

PHP自动化Wordpress安装程序使用shell_exec()加速过程

I'm trying to make and automated installer for my platform that download lastest wordpress tar and untar directly in the member directory, then remove all the temp files.

<?php
$dir = $_GET['username'];
if ($dir != null ) {
   print 'Downloading...';
   shell_exec('wget https://wordpress.org/latest.tar.gz -P /home/ubuntu/workspace/members/' . $dir);
   sleep(10);
   print 'OK | ';
   print 'Extracting...';
   shell_exec('tar xvzf /home/ubuntu/workspace/members/' . $dir . '/lastest.tar.gz -C /home/ubuntu/workspace/members/' . $dir);
   sleep(20);
   print 'OK | ';
   print 'Moving to root...';
   shell_exec('mv /home/ubuntu/workspace/members/' . $dir . '/wordpress /home/ubuntu/workspace/members/' . $dir);
   sleep(20);
   print 'OK | ';
   print 'Directory polish...';
   shell_exec('rm /home/ubuntu/workspace/members/' .$dir . '/wordpress | rm /home/ubuntu/workspace/members' . $dir . '/lastest.tar.gz');
   print 'OK | DONE.';
} else {
   print 'Error in get member name';
}

This work fine, latest file where downloaded and extracted, files where moved etc.

I would like to speedup the execution of this script, i've put the sleep()to make sure commands are not executed after the previous is done but I don't know if the system is loaded so I've put much time to wait.

There is a way to exactly know when a process is complete and start next process? will I must to put all in a line using the command separator |? Or there is an easy way to make it faster?

EDIT: According to the Replies it's easy to make this working faster removing the sleep()commands, but this script is pretty unsecure, so how can I make this more secure to execute?

  • 写回答

1条回答 默认 最新

  • douyan1321 2017-03-10 10:23
    关注

    shell_exec does not return until the executing command is complete - which you can verify yourself by running

    <?php
    shell_exec("sleep 3");
    echo "done";
    

    and seeing how long it takes.

    Meaning: you do not have to sleep at all.

    That being said: using user input directly in a system command is a huge huge security risk. any attacker could inject malicious code and completely compromise your server without lots of effort. a complete loss of data would be the least threatening scenario.

    As a baseline: never trust user input!. to make your current code more secure: verify the input - for example, check if it is only alphanumeric, if it's a valid username, if it's the name of the currently logged in user, if the folder actually exists, and so on.
    Best would be to store a sanitized path to your user directory in a database and look it up (with parameterized statements, lest you open yourself to SQL Injection instead).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里