function put_contents($file, $contents) { $result = false; if ($ftp = AkFtp::connect()) { $file = str_replace('\\', '/', $file); $path = dirname($file); if (!AkFtp::is_dir($path)) { AkFtp::make_dir($path); } $tmpfname = tempnam('/tmp', 'tmp'); $temp = fopen($tmpfname, 'a+'); fwrite($temp, $contents); fclose($temp); $temp = fopen($tmpfname, 'rb'); $result = ftp_fput($ftp, $file, $temp, FTP_BINARY); fclose($temp); unlink($tmpfname); } return $result; }
static function file_put_contents($file_name, $content, $options = array()) { $default_options = array('ftp' => defined('AK_UPLOAD_FILES_USING_FTP') && AK_UPLOAD_FILES_USING_FTP, 'base_path' => self::getDefaultBasePath($file_name)); $options = array_merge($default_options, $options); $file_name = self::getRestrictedPath($file_name, $options); if ($options['ftp']) { if (!AkFtp::is_dir(dirname($file_name))) { AkFtp::make_dir(dirname($file_name)); } return AkFtp::put_contents($file_name, $content); } else { $base_path = self::getNormalizedBasePath($options); if (!is_dir(dirname($base_path . $file_name))) { self::make_dir(dirname($base_path . $file_name), $options); } if (!($result = file_put_contents($base_path . $file_name, $content))) { if (!empty($content)) { trigger_error(Ak::t("Could not write to file: %file_name. Please change file/dir permissions or enable FTP file handling on your Akelos application.", array('%file_name' => '"' . $base_path . $file_name . '"')), E_USER_ERROR); } } return $result; } }
public function Test_is_dir() { $path = 'invalid path'; $this->assertFalse(AkFtp::is_dir($path)); $path = 'this_is_a_file.txt'; Ak::file_put_contents('this_is_a_file.txt', ''); $this->assertFalse(AkFtp::is_dir($path)); AkFtp::make_dir('tmp_test_dir'); Ak::file_put_contents('tmp_test_dir/file_inside.txt', ''); $path = 'tmp_test_dir/file_inside.txt'; $this->assertFalse(AkFtp::is_dir($path)); AkFtp::make_dir('real_dir/another/dir'); $path = 'real_dir'; $this->assertTrue(AkFtp::is_dir($path)); $path = 'real_dir/another/dir'; $this->assertTrue(AkFtp::is_dir($path)); AkFtp::delete('real_dir'); AkFtp::delete('this_is_a_file.txt'); AkFtp::delete('tmp_test_dir'); }
function file_put_contents($file_name, $content, $options = array()) { $default_options = array('ftp' => defined('AK_UPLOAD_FILES_USING_FTP') && AK_UPLOAD_FILES_USING_FTP, 'base_path' => AK_BASE_DIR); $options = array_merge($default_options, $options); if (!function_exists('file_put_contents')) { include_once AK_CONTRIB_DIR . DS . 'pear' . DS . 'PHP' . DS . 'Compat.php'; PHP_Compat::loadFunction(array('file_put_contents')); } $file_name = trim(str_replace($options['base_path'], '', $file_name), DS); if ($options['ftp']) { require_once AK_LIB_DIR . DS . 'AkFtp.php'; $file_name = trim(str_replace(array(DS, '//'), array('/', '/'), $file_name), '/'); if (!AkFtp::is_dir(dirname($file_name))) { AkFtp::make_dir(dirname($file_name)); } return AkFtp::put_contents($file_name, $content); } else { if (!is_dir(dirname($options['base_path'] . DS . $file_name))) { Ak::make_dir(dirname($options['base_path'] . DS . $file_name), $options); } if (!($result = file_put_contents($options['base_path'] . DS . $file_name, $content))) { if (!empty($content)) { Ak::trace("Could not write to file: \"" . $options['base_path'] . DS . "{$file_name}\". Please change file/dir permissions or enable FTP file handling by" . " setting the following on your config/" . AK_ENVIRONMENT . ".php file \n<pre>define('AK_UPLOAD_FILES_USING_FTP', true);\n" . "define('AK_READ_FILES_USING_FTP', false);\n" . "define('AK_DELETE_FILES_USING_FTP', true);\n" . "define('AK_FTP_PATH', 'ftp://*****:*****@example.com/path_to_the_framework');\n" . "define('AK_FTP_AUTO_DISCONNECT', true);\n</pre>"); } } return $result; } }
public function test_is_dir() { if (!RUN_FTP_TESTS) { return; } $path = 'invalid path'; $this->assertFalse(AkFtp::is_dir($path)); $path = 'this_is_a_file.txt'; AkFileSystem::file_put_contents('this_is_a_file.txt', ''); $this->assertFalse(AkFtp::is_dir($path)); AkFtp::make_dir('tmp_test_dir'); AkFileSystem::file_put_contents('tmp_test_dir/file_inside.txt', ''); $path = 'tmp_test_dir/file_inside.txt'; $this->assertFalse(AkFtp::is_dir($path)); AkFtp::make_dir('real_dir/another/dir'); $path = 'real_dir'; $this->assertTrue(AkFtp::is_dir($path)); $path = 'real_dir/another/dir'; $this->assertTrue(AkFtp::is_dir($path)); AkFtp::delete('real_dir'); AkFtp::delete('this_is_a_file.txt'); AkFtp::delete('tmp_test_dir'); AkFtp::delete('cache'); }
static function file_put_contents($file_name, $content, $options = array()) { $default_options = array('ftp' => defined('AK_UPLOAD_FILES_USING_FTP') && AK_UPLOAD_FILES_USING_FTP, 'base_path' => strstr($file_name, AK_TMP_DIR) ? AK_TMP_DIR : AK_BASE_DIR); $options = array_merge($default_options, $options); $file_name = trim(str_replace($options['base_path'], '', $file_name), DS); if ($options['ftp']) { $file_name = trim(str_replace(array(DS, '//'), array('/', '/'), $file_name), '/'); if (!AkFtp::is_dir(dirname($file_name))) { AkFtp::make_dir(dirname($file_name)); } return AkFtp::put_contents($file_name, $content); } else { $base_path = AK_WIN && empty($options['base_path']) ? '' : $options['base_path'] . DS; if (!is_dir(dirname($base_path . $file_name))) { Ak::make_dir(dirname($base_path . $file_name), $options); } if (!($result = file_put_contents($base_path . $file_name, $content))) { if (!empty($content)) { trigger_error(Ak::t("Could not write to file: %file_name. Please change file/dir permissions or enable FTP file handling on your Akelos application.", array('%file_name' => '"' . $base_path . $file_name . '"')), E_USER_ERROR); } } return $result; } }
static function delete($path, $only_files = false) { $result = false; if ($ftp_conn = AkFtp::connect()) { $path = AkFtp::unrelativizePath($path); $path = str_replace('\\', '/', $path); $path = str_replace(array('..', './'), array('', ''), $path); $keep_parent_dir = substr($path, -2) != '/*'; $path = trim($path, '/*'); $list = AK_FTP_SHOW_ERRORS ? ftp_rawlist($ftp_conn, "-R {$path}") : @ftp_rawlist($ftp_conn, "-R {$path}"); $dirs = $keep_parent_dir ? array($path) : array(); $files = array($path); $current_dir = $path . '/'; if (count($list) === 1 && !AkFtp::is_dir($path)) { $dirs = array(); $files[] = $path; } else { foreach ($list as $k => $line) { if (substr($line, -1) == ':') { $current_dir = substr($line, 0, strlen($line) - 1) . '/'; } if (preg_match("/([-d][rwxst-]+).* ([0-9]) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)/", $line, $regs)) { if (substr($regs[1], 0, 1) == "d") { if ($regs[8] != '.' && $regs[8] != '..') { $dirs[] = $current_dir . $regs[8]; } } else { $files[] = $current_dir . $regs[8]; } } } } if (count($files) >= 1) { array_shift($files); } rsort($dirs); foreach ($files as $file) { if (!($result = @ftp_delete($ftp_conn, $file))) { trigger_error(Ak::t('Could not delete FTP file %file_path', array('%file_path' => $file)), E_USER_NOTICE); return false; } } if (!$only_files) { foreach ($dirs as $dir) { if (!($result = @ftp_rmdir($ftp_conn, $dir))) { trigger_error(Ak::t('Could not delete FTP directory %dir_path', array('%dir_path' => $dir)), E_USER_NOTICE); return false; } } } } return $result; }