Author: Nick Sagona, III (nick@popphp.org)
Beispiel #1
0
 /**
  * Method to create the hashed value
  *
  * @param  string $string
  * @return string
  */
 public function create($string)
 {
     $hash = null;
     $this->salt = null === $this->salt ? substr(str_replace('+', '.', base64_encode(String::random(32))), 0, 9) : substr(str_replace('+', '.', base64_encode($this->salt)), 0, 9);
     $hash = crypt($string, '$1$' . $this->salt);
     return $hash;
 }
Beispiel #2
0
 /**
  * Install the model class files
  *
  * @param \Pop\Config $install
  * @return void
  */
 public static function install($install)
 {
     echo \Pop\I18n\I18n::factory()->__('Creating model class files...') . PHP_EOL;
     // Create model class folder
     $modelDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Model';
     if (!file_exists($modelDir)) {
         mkdir($modelDir);
     }
     $models = $install->models->asArray();
     foreach ($models as $model) {
         $modelName = ucfirst(\Pop\Filter\String::underscoreToCamelcase($model));
         // Define namespace
         $ns = new NamespaceGenerator($install->project->name . '\\Model');
         // Create and save model class file
         $modelCls = new Generator($modelDir . '/' . $modelName . '.php', Generator::CREATE_CLASS);
         $modelCls->setNamespace($ns);
         $modelCls->save();
     }
 }
Beispiel #3
0
 /**
  * Install the table class files
  *
  * @param \Pop\Config $install
  * @param array  $dbTables
  * @return void
  */
 public static function install($install, $dbTables)
 {
     echo \Pop\I18n\I18n::factory()->__('Creating database table class files...') . PHP_EOL;
     // Create table class folder
     $tableDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Table';
     if (!file_exists($tableDir)) {
         mkdir($tableDir);
     }
     // Loop through the tables, creating the classes
     foreach ($dbTables as $table => $value) {
         $prefix = isset($value['prefix']) ? $value['prefix'] : null;
         $tableName = ucfirst(\Pop\Filter\String::underscoreToCamelcase(str_replace($prefix, '', $table)));
         $ns = new NamespaceGenerator($install->project->name . '\\Table');
         $ns->setUse('Pop\\Db\\Record');
         if (strpos($value['primaryId'], '|') !== false) {
             $pIdType = 'array';
             $pId = explode('|', $value['primaryId']);
         } else {
             $pIdType = 'string';
             $pId = $value['primaryId'];
         }
         if (null !== $prefix) {
             $prefix = new PropertyGenerator('prefix', 'string', $prefix, 'protected');
         }
         $propId = new PropertyGenerator('primaryId', $pIdType, $pId, 'protected');
         $propAuto = new PropertyGenerator('auto', 'boolean', $value['auto'], 'protected');
         // Create and save table class file
         $tableCls = new Generator($tableDir . '/' . $tableName . '.php', Generator::CREATE_CLASS);
         $tableCls->setNamespace($ns);
         $tableCls->code()->setParent('Record')->addProperty($propId)->addProperty($propAuto);
         if (null !== $prefix) {
             $tableCls->code()->addProperty($prefix);
         }
         $tableCls->save();
     }
 }
Beispiel #4
0
 public function testUnderscoreToSeparator()
 {
     $this->assertEquals('hello' . DIRECTORY_SEPARATOR . 'world', String::underscoreToSeparator('hello_world'));
 }
Beispiel #5
0
 /**
  * Send password reminder to user
  *
  * @param  string      $email
  * @param  \Pop\Config $config
  * @return void
  */
 public function sendReminder($email, $config)
 {
     $encOptions = $config->encryptionOptions->asArray();
     $user = Table\Users::findBy(array('email' => $email));
     if (isset($user->id)) {
         $type = Table\UserTypes::findById($user->type_id);
         if ($type->password_encryption == Auth\Auth::ENCRYPT_NONE) {
             $newPassword = $this->password;
             $newEncPassword = $newPassword;
             $msg = $this->i18n->__('Your username and password is:');
         } else {
             $newPassword = (string) String::random(8, String::ALPHANUM);
             $newEncPassword = self::encryptPassword($newPassword, $type->password_encryption, $encOptions);
             $msg = $this->i18n->__('Your password has been reset for security reasons. Your username and new password is:');
         }
         // Save new password
         $user->password = $newEncPassword;
         $user->save();
         // Get base path and domain
         $basePath = strtolower($type->type) != 'user' ? BASE_PATH . '/' . strtolower($type->type) : BASE_PATH . APP_URI;
         $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
         // Set recipient
         $rcpt = array('name' => $user->username, 'email' => $user->email, 'username' => $user->username, 'password' => $newPassword, 'login' => 'http://' . $_SERVER['HTTP_HOST'] . $basePath . '/login', 'domain' => $domain, 'message' => $msg);
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/themes/phire/mail/forgot.txt')) {
             $mailTmpl = file_get_contents($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/themes/phire/mail/forgot.txt');
         } else {
             $mailTmpl = file_get_contents(__DIR__ . '/../../../view/phire/mail/forgot.txt');
         }
         $mailTmpl = str_replace(array('Dear', 'Here is your password for', 'You can login at:', 'Thank You'), array($this->i18n->__('Dear'), $this->i18n->__('Here is your password for'), $this->i18n->__('You can login at:'), $this->i18n->__('Thank You')), $mailTmpl);
         // Send reminder
         $mail = new Mail($domain . ' - ' . $this->i18n->__('Password Reset'), $rcpt);
         $mail->from(Table\Config::findById('reply_email')->value);
         $mail->setText($mailTmpl);
         $mail->send();
     }
 }
Beispiel #6
0
 /**
  * Method to create the hashed value
  *
  * @param  string $string
  * @throws Exception
  * @return string
  */
 public function create($string)
 {
     $hash = null;
     $prefix = $this->bits == 512 ? '$6$' : '$5$';
     $prefix .= 'rounds=' . $this->rounds . '$';
     $this->salt = null === $this->salt ? substr(str_replace('+', '.', base64_encode(String::random(32))), 0, 16) : substr(str_replace('+', '.', base64_encode($this->salt)), 0, 16);
     $hash = crypt($string, $prefix . $this->salt);
     return $hash;
 }
Beispiel #7
0
<?php

require_once '../../bootstrap.php';
use Pop\Filter\String;
try {
    $html = 'Some text, http://www.google.com/ and also https://www.google.com/ and someone@email.com and ftp://ftp.someserver.com';
    echo 'Links: ' . String::links($html, true) . '<br /><br />' . PHP_EOL;
    echo 'Slug: ' . String::slug('Testing, 1, 2, 3 | About Us | Hello World!', ' | ') . '<br /><br />' . PHP_EOL;
    echo 'Random String: ' . String::random(6, String::ALPHANUM, String::UPPER) . '<br /><br />' . PHP_EOL;
} catch (\Exception $e) {
    echo $e->getMessage();
}
Beispiel #8
0
 /**
  * Method to create the hashed value
  *
  * @param  string $string
  * @throws Exception
  * @return string
  */
 public function create($string)
 {
     $hash = null;
     $this->salt = null === $this->salt ? substr(str_replace('+', '.', base64_encode(String::random(32))), 0, 22) : substr(str_replace('+', '.', base64_encode($this->salt)), 0, 22);
     $hash = crypt($string, $this->prefix . $this->cost . '$' . $this->salt);
     if (strlen($hash) < 13) {
         throw new Exception('Error: There was an error with the bcrypt generation.');
     }
     return $hash;
 }
Beispiel #9
0
 /**
  * Method to create the hashed value
  *
  * @param  string $string
  * @return string
  */
 public function create($string)
 {
     $hash = null;
     $this->ivSize = mcrypt_get_iv_size($this->cipher, $this->mode);
     $this->salt = null === $this->salt ? substr(str_replace('+', '.', base64_encode(String::random(32))), 0, $this->ivSize) : substr(str_replace('+', '.', base64_encode($this->salt)), 0, $this->ivSize);
     $this->iv = mcrypt_create_iv($this->ivSize, $this->source);
     $hash = mcrypt_encrypt($this->cipher, $this->salt, $string, $this->mode, $this->iv);
     $hash = base64_encode($this->iv . $this->salt . '$' . $hash);
     return $hash;
 }
 /**
  * Create the bootstrap file
  *
  * @param \Pop\Config $install
  * @return void
  */
 public static function install($install)
 {
     // Define full paths of the autoloader and config files
     $autoload = realpath(__DIR__ . '/../../Loader/Autoloader.php');
     $moduleSrc = realpath($install->project->base . '/module/' . $install->project->name . '/src');
     $projectCfg = realpath($install->project->base . '/config/project.php');
     $moduleCfg = realpath($install->project->base . '/module/' . $install->project->name . '/config/module.php');
     // Figure out the relative base and docroot
     $base = str_replace("\\", '/', realpath($install->project->base));
     $docroot = str_replace("\\", '/', realpath($install->project->docroot));
     $base = substr($base, -1) == '/' ? substr($base, 0, -1) : $base;
     $docroot = substr($docroot, -1) == '/' ? substr($docroot, 0, -1) : $docroot;
     // If the base and docroot are the same
     if (strlen($base) == strlen($docroot)) {
         $autoload = "__DIR__ . '/vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
         $moduleSrc = "__DIR__ . '/module/" . $install->project->name . "/src'";
         $projectCfg = "__DIR__ . '/config/project.php'";
         $moduleCfg = "__DIR__ . '/module/" . $install->project->name . "/config/module.php'";
         // If the docroot is under the base
     } else {
         if (strlen($base) < strlen($docroot)) {
             // Calculate how many levels up the base is from the docroot
             $diff = str_replace($base, '', $docroot);
             $levels = substr_count($diff, '/');
             $dirs = '/';
             for ($i = 0; $i < $levels; $i++) {
                 $dirs .= '../';
             }
             $autoload = "__DIR__ . '" . $dirs . "vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
             $moduleSrc = "__DIR__ . '" . $dirs . "module/" . $install->project->name . "/src'";
             $projectCfg = "__DIR__ . '" . $dirs . "config/project.php'";
             $moduleCfg = "__DIR__ . '" . $dirs . "module/" . $install->project->name . "/config/module.php'";
             // If the base is under the docroot
         } else {
             if (strlen($base) > strlen($docroot)) {
                 $dir = str_replace($docroot, '', $base);
                 $autoload = "__DIR__ . '" . $dir . "/vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
                 $moduleSrc = "__DIR__ . '" . $dir . "/module/" . $install->project->name . "/src'";
                 $projectCfg = "__DIR__ . '" . $dir . "/config/project.php'";
                 $moduleCfg = "__DIR__ . '" . $dir . "/module/" . $install->project->name . "/config/module.php'";
             }
         }
     }
     // Create new Code file object
     $bootstrap = new \Pop\Code\Generator($install->project->docroot . '/bootstrap.php');
     // Create new bootstrap file
     if (!file_exists($install->project->docroot . '/bootstrap.php')) {
         $bootstrap->appendToBody("// Require the Autoloader class file" . PHP_EOL . "require_once {$autoload};" . PHP_EOL)->appendToBody("// Instantiate the autoloader object" . PHP_EOL . "\$autoloader = Pop\\Loader\\Autoloader::factory();" . PHP_EOL . "\$autoloader->splAutoloadRegister();");
     }
     // Else, just append to the existing bootstrap file
     $bootstrap->appendToBody("\$autoloader->register('{$install->project->name}', {$moduleSrc});" . PHP_EOL)->appendToBody("// Create a project object")->appendToBody("\$project = {$install->project->name}\\Project::factory(")->appendToBody("    include {$projectCfg},")->appendToBody("    include {$moduleCfg},");
     // Set up any controllers via a router object
     if (isset($install->controllers)) {
         $controllers = $install->controllers->asArray();
         $ctrls = array();
         foreach ($controllers as $key => $value) {
             $subs = array();
             foreach ($value as $k => $v) {
                 if (is_array($v)) {
                     $subs[] = $k;
                 }
             }
             if (count($subs) > 0) {
                 $ctls = "'{$key}' => array(" . PHP_EOL;
                 if (array_key_exists('index', $value)) {
                     $ctls .= "            '/' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase(substr($key, 1))) . "\\IndexController'," . PHP_EOL;
                 }
                 foreach ($subs as $sub) {
                     $ctls .= "            '{$sub}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase(substr($key, 1))) . "\\" . ucfirst(String::underscoreToCamelcase(substr($sub, 1))) . "Controller'," . PHP_EOL;
                 }
                 $ctls .= '        )';
                 $ctrls[] = $ctls;
             } else {
                 if ($key == '/') {
                     $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\IndexController'";
                 } else {
                     $controllerName = substr($key, 1);
                     if (array_key_exists('index', $value)) {
                         $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase($controllerName)) . "\\IndexController'";
                     } else {
                         $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase($controllerName)) . "Controller'";
                     }
                 }
             }
         }
         $bootstrap->appendToBody("    new Pop\\Mvc\\Router(array(");
         $i = 1;
         foreach ($ctrls as $c) {
             $end = $i < count($ctrls) ? ',' : null;
             $bootstrap->appendToBody("        " . $c . $end);
             $i++;
         }
         $bootstrap->appendToBody("    ))");
     }
     // Finalize and save the bootstrap file
     $bootstrap->appendToBody(");")->save();
 }
Beispiel #11
0
 /**
  * Install the form class files
  *
  * @param \Pop\Config $install
  * @return void
  */
 public static function install($install)
 {
     echo \Pop\I18n\I18n::factory()->__('Creating form class files...') . PHP_EOL;
     // Create form class folder
     $formDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Form';
     if (!file_exists($formDir)) {
         mkdir($formDir);
     }
     $forms = $install->forms->asArray();
     foreach ($forms as $name => $form) {
         $formName = ucfirst(\Pop\Filter\String::underscoreToCamelcase($name));
         // Define namespace
         $ns = new NamespaceGenerator($install->project->name . '\\Form');
         $ns->setUse('Pop\\Form\\Form')->setUse('Pop\\Form\\Element')->setUse('Pop\\Validator');
         // Create the constructor
         $construct = new MethodGenerator('__construct');
         $construct->setDesc('Constructor method to instantiate the form object');
         $construct->getDocblock()->setReturn('self');
         $construct->addArguments(array(array('name' => 'action', 'value' => 'null', 'type' => 'string'), array('name' => 'method', 'value' => "'post'", 'type' => 'string'), array('name' => 'fields', 'value' => 'null', 'type' => 'array'), array('name' => 'indent', 'value' => 'null', 'type' => 'string')));
         // Create the init values array within the constructor
         if (is_array($form) && count($form) > 0) {
             $construct->appendToBody("\$this->initFieldsValues = array (");
             $i = 0;
             foreach ($form as $name => $field) {
                 $i++;
                 $construct->appendToBody("    '" . $name . "' => array (");
                 $j = 0;
                 foreach ($field as $key => $value) {
                     $j++;
                     $comma = $j < count($field) ? ',' : null;
                     if ($key == 'validators') {
                         $val = null;
                         if (is_array($value)) {
                             $val = 'array(' . PHP_EOL;
                             foreach ($value as $v) {
                                 $val .= '            new Validator\\' . $v . ',' . PHP_EOL;
                             }
                             $val .= '        )';
                         } else {
                             $val = 'new Validator\\' . $value;
                         }
                         $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                     } else {
                         if ($key == 'value' || $key == 'marked' || $key == 'attributes' || $key == 'error') {
                             $val = var_export($value, true);
                             $val = str_replace(PHP_EOL, PHP_EOL . '        ', $val);
                             if (strpos($val, 'Select::') !== false) {
                                 $val = 'Element\\' . str_replace("'", '', $val);
                             }
                             $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                         } else {
                             if (is_bool($value)) {
                                 $val = $value ? 'true' : 'false';
                             } else {
                                 $val = "'" . $value . "'";
                             }
                             $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                         }
                     }
                 }
                 $end = $i < count($form) ? '    ),' : '    )';
                 $construct->appendToBody($end);
             }
             $construct->appendToBody(");");
         }
         $construct->appendToBody("parent::__construct(\$action, \$method, \$fields, \$indent);");
         // Create and save form class file
         $formCls = new Generator($formDir . '/' . $formName . '.php', Generator::CREATE_CLASS);
         $formCls->setNamespace($ns);
         $formCls->code()->setParent('Form')->addMethod($construct);
         $formCls->save();
     }
 }
Beispiel #12
0
 /**
  * Get the init field values
  *
  * @param  int     $tid
  * @param  boolean $profile
  * @param  int     $uid
  * @param  string  $action
  * @param  boolean $register
  * @return array
  */
 protected function getInitFields($tid = 0, $profile = false, $uid = 0, $action, $register = false)
 {
     $type = Table\UserTypes::findById($tid);
     $fields1 = array();
     // Continue setting up initial user fields
     $fields1['email1'] = array('type' => 'text', 'label' => $this->i18n->__('Email'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\Email());
     if ($type->email_verification) {
         $fields1['email2'] = array('type' => 'text', 'label' => $this->i18n->__('Re-Type Email'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\Email());
     }
     // If not email as username, create username field
     if (!$type->email_as_username) {
         $fields2 = array('username' => array('type' => 'text', 'label' => $this->i18n->__('Username'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => array(new Validator\AlphaNumeric(), new Validator\LengthGte(4))));
         if ($uid != 0) {
             $fields2['username']['attributes']['onkeyup'] = "phire.updateTitle('#username-title', this);";
         }
     } else {
         $fields2 = array();
         if ($uid != 0) {
             $fields1['email1']['attributes']['onkeyup'] = "phire.updateTitle('#username-title', this);";
         }
     }
     // Continue setting up initial user fields
     if ($type->login) {
         $fields3 = array('password1' => array('type' => 'password', 'label' => $this->i18n->__('Enter Password'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\LengthGte(6)), 'password2' => array('type' => 'password', 'label' => $this->i18n->__('Re-Type Password'), 'required' => true, 'attributes' => array('size' => 30), 'validators' => new Validator\LengthGte(6)));
     } else {
         $fields3 = array();
     }
     $fieldGroups = array();
     $dynamicFields = false;
     $model = str_replace('Form', 'Model', get_class($this));
     $newFields = \Phire\Model\Field::getByModel($model, $tid, $uid);
     if ($newFields['dynamic']) {
         $dynamicFields = true;
     }
     if ($newFields['hasFile']) {
         $this->hasFile = true;
     }
     foreach ($newFields as $key => $value) {
         if (is_numeric($key)) {
             $fieldGroups[] = $value;
         }
     }
     $fields4 = array();
     if ($register) {
         $site = Table\Sites::getSite();
         if ($type->use_csrf) {
             $fields4['csrf'] = array('type' => 'csrf', 'value' => \Pop\Filter\String::random(8));
         }
         if ($type->use_captcha) {
             $fields4['captcha'] = array('type' => 'captcha', 'label' => $this->i18n->__('Enter Code'), 'captcha' => '<br /><img id="captcha-image" src="' . $site->base_path . '/captcha" /><br /><a class="reload-link" href="#" onclick="document.getElementById(\'captcha-image\').src = \'' . $site->base_path . '/captcha?reload=1\';return false;">' . $this->i18n->__('Reload') . '</a>', 'attributes' => array('size' => 5));
         }
     }
     // Finish the initial fields
     $fields4['submit'] = array('type' => 'submit', 'value' => strpos($action, '/register') !== false ? $this->i18n->__('REGISTER') : $this->i18n->__('SAVE'), 'attributes' => array('class' => strpos($action, '/install/user') !== false || $profile ? 'update-btn' : 'save-btn'));
     if ($profile) {
         $fields4['submit']['label'] = '&nbsp;';
         $fields4['submit']['attributes']['style'] = 'width: 250px;';
         $fields4['profile'] = array('type' => 'hidden', 'value' => 1);
         $sess = \Pop\Web\Session::getInstance();
         if (isset($sess->reset_pwd)) {
             $fields4['reset_pwd'] = array('type' => 'hidden', 'value' => 1);
         }
     }
     if (!$profile) {
         $fields4['update'] = array('type' => 'button', 'value' => $this->i18n->__('Update'), 'attributes' => array('onclick' => "return phire.updateForm('#user-form', " . ($this->hasFile || $dynamicFields ? 'true' : 'false') . ");", 'class' => 'update-btn'));
     }
     $fields4['type_id'] = array('type' => 'hidden', 'value' => $tid);
     $fields4['id'] = array('type' => 'hidden', 'value' => 0);
     if (!$profile) {
         $fields4['update_value'] = array('type' => 'hidden', 'value' => 0);
     }
     // If not profile
     if (!$profile) {
         // Get roles for user type
         $rolesAry = array('0' => '(' . $this->i18n->__('Blocked') . ')');
         if ($tid != 0) {
             $roles = Table\UserRoles::findBy(array('type_id' => $tid), 'id ASC');
             foreach ($roles->rows as $role) {
                 $rolesAry[$role->id] = $role->name;
             }
         }
         $siteIds = array('0' => $_SERVER['HTTP_HOST']);
         $sites = Table\Sites::findAll();
         foreach ($sites->rows as $site) {
             $siteIds[(string) $site->id] = $site->domain;
         }
         $fields4['role_id'] = array('type' => 'select', 'required' => true, 'label' => $this->i18n->__('User Role'), 'value' => $rolesAry, 'marked' => $type->default_role_id);
         $fields4['verified'] = array('type' => 'select', 'label' => $this->i18n->__('Verified'), 'value' => array('1' => $this->i18n->__('Yes'), '0' => $this->i18n->__('No')), 'marked' => '0');
         $fields4['failed_attempts'] = array('type' => 'text', 'label' => $this->i18n->__('Failed Attempts'), 'attributes' => array('size' => 3));
         $fields4['site_ids'] = array('type' => 'checkbox', 'label' => $this->i18n->__('Allowed Sites'), 'value' => $siteIds);
     }
     if (strpos($action, '/install/user') !== false || $profile) {
         $allFields = array($fields1, $fields2, $fields3);
         if (count($fieldGroups) > 0) {
             foreach ($fieldGroups as $fg) {
                 $allFields[] = $fg;
             }
         }
         $allFields[] = $fields4;
     } else {
         $allFields = array($fields4, $fields1, $fields2, $fields3);
         if (count($fieldGroups) > 0) {
             foreach ($fieldGroups as $fg) {
                 $allFields[] = $fg;
             }
         }
     }
     return $allFields;
 }