/** * Static method to generate a class map file * * @param string $inputFolder * @param string $outputFile * @return void */ public static function generate($inputFolder, $outputFile) { $dir = new \Pop\File\Dir(realpath($inputFolder), true, true); $matches = array(); $files = $dir->getFiles(); foreach ($files as $file) { if (substr($file, -4) == '.php') { $classMatch = array(); $namespaceMatch = array(); $classFileContents = file_get_contents($file); preg_match('/^class(.*)$/m', $classFileContents, $classMatch); preg_match('/^namespace(.*)$/m', $classFileContents, $namespaceMatch); $ary = array(); if (isset($classMatch[0])) { $ary['file'] = str_replace('\\', '/', $file); $ary['class'] = self::parseClass($classMatch[0]); $ary['namespace'] = isset($namespaceMatch[0]) ? self::parseNamespace($namespaceMatch[0]) : null; $matches[] = $ary; } } } if (count($matches) > 0) { $classMap = '<?php' . PHP_EOL . PHP_EOL . 'return array('; $i = 1; foreach ($matches as $class) { $comma = $i < count($matches) ? ',' : null; $className = null !== $class['namespace'] ? $class['namespace'] . '\\' . $class['class'] : $class['class']; $classMap .= PHP_EOL . ' \'' . $className . '\' => \'' . $class['file'] . '\'' . $comma; $i++; } $classMap .= PHP_EOL . ');' . PHP_EOL; file_put_contents($outputFile, $classMap); } }
/** * Get languages from the XML files. * * @param string $dir * @return array */ public static function getLanguages($dir = null) { $langsAry = array(); $langDirectory = null !== $dir ? $dir : __DIR__ . '/Data'; if (file_exists($langDirectory)) { $langDir = new \Pop\File\Dir($langDirectory); $files = $langDir->getFiles(); foreach ($files as $file) { if ($file != '__.xml') { if (($xml = @new \SimpleXMLElement($langDirectory . '/' . $file, LIBXML_NOWARNING, true)) !== false) { $lang = (string) $xml->attributes()->output; $langName = (string) $xml->attributes()->name; $langNative = (string) $xml->attributes()->native; foreach ($xml->locale as $locale) { $region = (string) $locale->attributes()->region; $name = (string) $locale->attributes()->name; $native = (string) $locale->attributes()->native; $native .= ' (' . $langName . ', ' . $name . ')'; $langsAry[$lang . '_' . $region] = $langNative . ', ' . $native; } } } } } ksort($langsAry); return $langsAry; }
/** * Post install command * * @return void */ public static function postInstall() { try { $ext = new Model\Extension(); $ext->getModules(); if (count($ext->new) > 0) { $ext->installModules(); } $dir = new \Pop\File\Dir(__DIR__ . '/../../../../..' . CONTENT_PATH . '/extensions/modules', true, true); foreach ($dir->getFiles() as $file) { chmod($file, 0777); } $dir = new \Pop\File\Dir(__DIR__ . '/../../../../..' . CONTENT_PATH . '/extensions/themes', true, true); foreach ($dir->getFiles() as $file) { chmod($file, 0777); } echo ' Installation Complete!' . PHP_EOL . PHP_EOL; } catch (\Exception $e) { echo ' ' . $e->getMessage() . PHP_EOL . PHP_EOL; } }
<?php /** * Module Name: phire-cache * Author: Nick Sagona * Description: This is the cache module for Phire CMS 2 * Version: 1.0 */ return ['phire-cache' => ['prefix' => 'Phire\\Cache\\', 'src' => __DIR__ . '/../src', 'routes' => include 'routes.php', 'resources' => include 'resources.php', 'nav.module' => ['name' => 'Cache Config', 'href' => '/cache', 'acl' => ['resource' => 'cache', 'permission' => 'index']], 'events' => [['name' => 'app.route.post', 'action' => 'Phire\\Cache\\Event\\Cache::load', 'priority' => 1000], ['name' => 'app.dispatch.post', 'action' => 'Phire\\Cache\\Event\\Cache::save', 'priority' => 1000]], 'install' => function () { mkdir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache'); chmod($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache', 0777); copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/index.html', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache/index.html'); chmod($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache/index.html', 0777); }, 'uninstall' => function () { $config = \Phire\Table\Config::findById('cache_adapter'); if (isset($config->setting)) { $config->delete(); } $config = \Phire\Table\Config::findById('cache_lifetime'); if (isset($config->setting)) { $config->delete(); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache')) { $dir = new \Pop\File\Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/cache'); $dir->emptyDir(true); } }, 'exclude' => []]];
/** * Method to clear all stored values from cache. * * @return void */ public function clear() { $dir = new \Pop\File\Dir($this->dir); $dir->emptyDir(); }
/** * Install the database * * @param string $dbname * @param array $db * @param string $dir * @param mixed $install * @param boolean $suppress * @param boolean $clear * @throws Exception * @return array */ public static function install($dbname, $db, $dir, $install = null, $suppress = false, $clear = true) { // Detect any SQL files $sqlFiles = array(); if (is_string($dir) && file_exists($dir) && strtolower(substr($dir, -4)) == '.sql') { $sqlFiles[] = $dir; } else { $dir = new \Pop\File\Dir($dir, true); $files = $dir->getFiles(); foreach ($files as $file) { if (strtolower(substr($file, -4)) == '.sql') { $sqlFiles[] = $file; } } } // If SQLite, create folder and empty SQLite file if (stripos($db['type'], 'sqlite') !== false) { if (is_string($install) && file_exists($install)) { $db['database'] = $install; } else { // Define folders to create $folders = array($install->project->base, $install->project->base . '/module', $install->project->base . '/module/' . $install->project->name, $install->project->base . '/module/' . $install->project->name . '/data'); // Create the folders foreach ($folders as $folder) { if (!file_exists($folder)) { mkdir($folder); } } // Create empty SQLite file and make file and folder writable chmod($install->project->base . '/module/' . $install->project->name . '/data', 0777); touch($install->project->base . '/module/' . $install->project->name . '/data/' . $db['database']); chmod($install->project->base . '/module/' . $install->project->name . '/data/' . $db['database'], 0777); $db['database'] = $install->project->base . '/module/' . $install->project->name . '/data/' . $db['database']; } } // Create DB connection if (stripos($db['type'], 'Pdo_') !== false) { $type = 'Pdo'; $db['type'] = strtolower(substr($db['type'], strpos($db['type'], '_') + 1)); } else { $type = $db['type']; } $popdb = Db::factory($type, $db); // If there are SQL files, parse them and execute the SQL queries if (count($sqlFiles) > 0) { if (!$suppress) { echo 'SQL files found. Executing SQL queries...' . PHP_EOL; } // Clear database if ($clear) { $oldTables = $popdb->adapter()->getTables(); if (count($oldTables) > 0) { if ($type == 'Mysqli' || $db['type'] == 'mysql') { $popdb->adapter()->query('SET foreign_key_checks = 0;'); foreach ($oldTables as $tab) { $popdb->adapter()->query("DROP TABLE " . $tab); } $popdb->adapter()->query('SET foreign_key_checks = 1;'); } else { if ($type == 'Pgsql' || $db['type'] == 'pgsql') { foreach ($oldTables as $tab) { $popdb->adapter()->query("DROP TABLE " . $tab . ' CASCADE'); } } else { foreach ($oldTables as $tab) { $popdb->adapter()->query("DROP TABLE " . $tab); } } } } } $prefix = isset($db['prefix']) ? $db['prefix'] : null; foreach ($sqlFiles as $sqlFile) { $sql = trim(file_get_contents($sqlFile)); $explode = strpos($sql, ";\r\n") !== false ? ";\r\n" : ";\n"; $statements = explode($explode, $sql); // Loop through each statement found and execute foreach ($statements as $s) { if (!empty($s)) { try { $popdb->adapter()->query(str_replace('[{prefix}]', $prefix, trim($s))); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; exit(0); } } } } } // Get table info $tables = array(); try { // Get Sqlite table info if (stripos($db['type'], 'sqlite') !== false) { $tablesFromDb = $popdb->adapter()->getTables(); if (count($tablesFromDb) > 0) { foreach ($tablesFromDb as $table) { $tables[$table] = array('primaryId' => null, 'auto' => false); $popdb->adapter()->query("PRAGMA table_info('" . $table . "')"); while (($row = $popdb->adapter()->fetch()) != false) { if ($row['pk'] == 1) { $tables[$table] = array('primaryId' => $row['name'], 'auto' => true); } } } } // Else, get MySQL, PgSQL and SQLSrv table info } else { if (stripos($db['type'], 'pgsql') !== false) { $schema = 'CATALOG'; $tableSchema = " AND TABLE_SCHEMA = 'public'"; $tableName = 'table_name'; $constraintName = 'constraint_name'; $columnName = 'column_name'; } else { if (stripos($db['type'], 'sqlsrv') !== false) { $schema = 'CATALOG'; $tableSchema = null; $tableName = 'TABLE_NAME'; $constraintName = 'CONSTRAINT_NAME'; $columnName = 'COLUMN_NAME'; } else { $schema = 'SCHEMA'; $tableSchema = null; $tableName = 'TABLE_NAME'; $constraintName = 'CONSTRAINT_NAME'; $columnName = 'COLUMN_NAME'; } } $popdb->adapter()->query("SELECT * FROM information_schema.TABLES WHERE TABLE_" . $schema . " = '" . $dbname . "'" . $tableSchema); // Get the auto increment info (mysql) and set table name while (($row = $popdb->adapter()->fetch()) != false) { $auto = !empty($row['AUTO_INCREMENT']) ? true : false; $tables[$row[$tableName]] = array('primaryId' => null, 'auto' => $auto); } // Get the primary key info foreach ($tables as $table => $value) { // Pgsql sequence info for auto increment if ($db['type'] == 'Pgsql') { $popdb->adapter()->query("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = '" . $table . "'"); $columns = array(); while (($row = $popdb->adapter()->fetch()) != false) { $columns[] = $row['column_name']; } if (count($columns) > 0) { foreach ($columns as $column) { $popdb->adapter()->query("SELECT pg_get_serial_sequence('" . $table . "', '" . $column . "')"); while (($row = $popdb->adapter()->fetch()) != false) { if (!empty($row['pg_get_serial_sequence'])) { $idAry = explode('_', $row['pg_get_serial_sequence']); if (isset($idAry[1]) && in_array($idAry[1], $columns)) { $tables[$table]['auto'] = true; } } } } } } // Get primary id, if there is one $ids = array(); $popdb->adapter()->query("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE CONSTRAINT_" . $schema . " = '" . $dbname . "' AND TABLE_NAME = '" . $table . "'"); while (($row = $popdb->adapter()->fetch()) != false) { if (isset($row[$constraintName])) { if (!isset($tables[$table]['primaryId'])) { $tables[$table]['primaryId'] = $row[$columnName]; } else { if (!in_array($row[$columnName], $ids)) { $tables[$table]['primaryId'] .= '|' . $row[$columnName]; } } $ids[] = $row[$columnName]; } } } } if (isset($db['prefix'])) { foreach ($tables as $table => $value) { $tables[$table]['prefix'] = $db['prefix']; } } } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; exit(0); } return $tables; }
/** * Send mail message or messages that are saved in a folder. * * This method depends on the server being set up correctly as an SMTP server * and sendmail being correctly defined in the php.ini file. * * @param string $from * @param boolean $delete * @return \Pop\Mail\Mail */ public function sendFrom($from = null, $delete = false) { $dir = null !== $from ? $from : getcwd(); $emailDir = new \Pop\File\Dir($dir, true); $emailFiles = $emailDir->getFiles(); if (isset($emailFiles[0])) { foreach ($emailFiles as $email) { if (file_exists($email)) { // Get the email data from the contents $emailData = $this->getEmailFromFile($email); // Send the email message. mail($emailData['to'], $emailData['subject'], $emailData['message'], $emailData['headers'], $this->params); // Delete the email file is the flag is passed if ($delete) { unlink($email); } } } } return $this; }