read() public method

Read data from a file.
public read ( integer | string $off = null, integer | string $len = null ) : string
$off integer | string
$len integer | string
return string
Esempio n. 1
0
 public function testDecode()
 {
     $f = new File(__DIR__ . '/../tmp/test.sql');
     $s = Sql::decode($f->read());
     $keys = array('id', 'username', 'password', 'email', 'access');
     $this->assertEquals(9, count($s));
     $this->assertEquals($keys, array_keys($s['row_1']));
 }
Esempio n. 2
0
 /**
  * Install config method
  *
  * @param mixed  $form
  * @param string $docRoot
  * @return void
  */
 public function config($form, $docRoot = null)
 {
     if (null === $docRoot) {
         $docRoot = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH;
     }
     // Get config file contents
     $cfgFile = new File($docRoot . '/config.php');
     $config = $cfgFile->read();
     // Get DB interface and type
     if (strpos($form->db_adapter, 'Pdo') !== false) {
         $dbInterface = 'Pdo';
         $dbType = strtolower(substr($form->db_adapter, strrpos($form->db_adapter, '\\') + 1));
     } else {
         $dbInterface = html_entity_decode($form->db_adapter, ENT_QUOTES, 'UTF-8');
         $dbType = null;
     }
     // If DB is SQLite
     if (strpos($form->db_adapter, 'Sqlite') !== false) {
         touch($docRoot . $form->content_path . '/.htphire.sqlite');
         $relativeDbName = "__DIR__ . '" . $form->content_path . '/.htphire.sqlite';
         $dbName = realpath($docRoot . $form->content_path . '/.htphire.sqlite');
         $dbUser = null;
         $dbPassword = null;
         $dbHost = null;
         $installFile = $dbName;
         chmod($dbName, 0777);
     } else {
         $relativeDbName = null;
         $dbName = $form->db_name;
         $dbUser = $form->db_username;
         $dbPassword = $form->db_password;
         $dbHost = $form->db_host;
         $installFile = null;
     }
     $dbPrefix = $form->db_prefix;
     // Set config values
     $config = str_replace("define('CONTENT_PATH', '/phire-content');", "define('CONTENT_PATH', '" . $form->content_path . "');", $config);
     $config = str_replace("define('APP_URI', '/phire');", "define('APP_URI', '" . $form->app_uri . "');", $config);
     $config = str_replace("define('DB_INTERFACE', '');", "define('DB_INTERFACE', '" . $dbInterface . "');", $config);
     $config = str_replace("define('DB_TYPE', '');", "define('DB_TYPE', '" . $dbType . "');", $config);
     $config = str_replace("define('DB_NAME', '');", "define('DB_NAME', " . (null !== $relativeDbName ? $relativeDbName : "'" . $dbName) . "');", $config);
     $config = str_replace("define('DB_USER', '');", "define('DB_USER', '" . $dbUser . "');", $config);
     $config = str_replace("define('DB_PASS', '');", "define('DB_PASS', '" . $dbPassword . "');", $config);
     $config = str_replace("define('DB_HOST', '');", "define('DB_HOST', '" . $dbHost . "');", $config);
     $config = str_replace("define('DB_PREFIX', '');", "define('DB_PREFIX', '" . $dbPrefix . "');", $config);
     $this->data['configWritable'] = is_writable($docRoot . '/config.php');
     if ($form instanceof \Pop\Form\Form) {
         // Store the config values in session in case config file is not writable.
         $sess = Session::getInstance();
         $sess->config = serialize(htmlentities($config, ENT_QUOTES, 'UTF-8'));
         $sess->app_uri = $form->app_uri;
     }
     if ($this->data['configWritable']) {
         $cfgFile->write($config)->save();
     }
     // Install the database
     $sqlFile = __DIR__ . '/../../../data/phire.' . str_replace(array('pdo\\', 'mysqli'), array('', 'mysql'), strtolower($form->db_adapter)) . '.sql';
     $db = array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'prefix' => $dbPrefix, 'type' => str_replace('\\', '_', $form->db_adapter));
     Dbs::install($dbName, $db, $sqlFile, $installFile, true);
     if (stripos($form->db_adapter, 'Pdo\\') !== false) {
         $adapter = 'Pdo';
         $type = strtolower(substr($form->db_adapter, strpos($form->db_adapter, '\\') + 1));
     } else {
         $adapter = $form->db_adapter;
         $type = null;
     }
     // Set the default system config
     $db = Db::factory($adapter, array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'type' => $type));
     // Get server info
     if (isset($_SERVER) && isset($_SERVER['SERVER_SOFTWARE'])) {
         $server = new Server();
         $os = $server->getOs() . ' (' . $server->getDistro() . ')';
         $srv = $server->getServer() . ' ' . $server->getServerVersion();
         $domain = $_SERVER['HTTP_HOST'];
         $doc = $_SERVER['DOCUMENT_ROOT'];
     } else {
         $os = '';
         $srv = '';
         $domain = '';
         $doc = '';
     }
     // Set the system configuration
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . \Phire\Project::VERSION . "' WHERE setting = 'system_version'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($domain) . "' WHERE setting = 'system_domain'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($doc) . "' WHERE setting = 'system_document_root'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($os) . "' WHERE setting = 'server_operating_system'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($srv) . "' WHERE setting = 'server_software'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->version() . "' WHERE setting = 'database_version'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . PHP_VERSION . "' WHERE setting = 'php_version'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . date('Y-m-d H:i:s') . "' WHERE setting = 'installed_on'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($form->language) . "' WHERE setting = 'default_language'");
     $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "user_types SET password_encryption = '" . $db->adapter()->escape((int) $form->password_encryption) . "' WHERE id = 2001");
 }
Esempio n. 3
0
         if (!array_key_exists($argv[2], $langs)) {
             echo $langsList . PHP_EOL;
             $lang = Install::getLanguage($langs);
         } else {
             $lang = $argv[2];
         }
     } else {
         echo $langsList . PHP_EOL;
         $lang = Install::getLanguage($langs);
     }
     $keys = array_keys($langs);
     echo 'You selected [' . $keys[$lang] . '] : ' . $langs[$keys[$lang]] . PHP_EOL . PHP_EOL;
     // Get the bootstrap file
     $location = Install::getBootstrap();
     $bootstrap = new File($location . '/bootstrap.php');
     $bootstrapCode = $bootstrap->read();
     // Set the new default language setting into the bootstrap file
     if (stripos($bootstrapCode, 'define(\'POP_LANG') !== false) {
         $curLangCode = substr($bootstrapCode, stripos($bootstrapCode, 'define(\'POP_LANG'));
         $curLangCode = substr($curLangCode, 0, strpos($curLangCode, ';'));
         $bootstrapCode = str_replace($curLangCode, 'define(\'POP_LANG\', \'' . $keys[$lang] . '\')', $bootstrapCode);
     } else {
         $curLangCode = substr($bootstrapCode, stripos($bootstrapCode, 'require_once'));
         $curLangCode = substr($curLangCode, 0, strpos($curLangCode, ';'));
         $langCode = PHP_EOL . '// Define the default language to use' . PHP_EOL . 'define(\'POP_LANG\', \'' . $keys[$lang] . '\');' . PHP_EOL . PHP_EOL . $curLangCode;
         $bootstrapCode = str_replace($curLangCode, $langCode, $bootstrapCode);
     }
     $bootstrap->write($bootstrapCode)->save();
     echo 'Done.' . PHP_EOL . PHP_EOL;
     // Else, generate class map
 } else {
Esempio n. 4
0
 public function testWriteSaveAndDelete()
 {
     if (file_exists(__DIR__ . '/../tmp/file.txt')) {
         unlink(__DIR__ . '/../tmp/file.txt');
     }
     $f = new File(__DIR__ . '/../tmp/file.txt');
     $f->write('123')->write('456', true)->save();
     $f->setPermissions(0777);
     $this->fileExists(__DIR__ . '/../tmp/file.txt');
     $this->assertEquals('123456', $f->read());
     $this->assertEquals(6, $f->getSize());
     $this->assertEquals('text/plain', $f->getMime());
     $f->delete();
     $this->assertFalse(file_exists(__DIR__ . '/../tmp/file.txt'));
 }