/** * Method to evaluate the validator * * @param mixed $input * @throws Exception * @return boolean */ public function evaluate($input = null) { // Check to make sure the input is a valid Ipv4 address. $ip = new Ipv4(); if (!Ipv4::factory()->evaluate($input)) { throw new Exception('The IP address must be a valid IPv4 address.'); } // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be part of the subnet %1.', $this->value); } else { $this->defaultMessage = I18n::factory()->__('The value must not be part of the subnet %1.', $this->value); } } // Evaluate the input against the validator if ((substr($this->input, 0, strrpos($this->input, '.')) == $this->value) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Constructor method to instantiate the default controller object * * @param Request $request * @param Response $response * @param Project $project * @param string $viewPath * @return self */ public function __construct(Request $request = null, Response $response = null, Project $project = null, $viewPath = null) { if (null === $viewPath) { $cfg = $project->module('Phire')->asArray(); $viewPath = __DIR__ . '/../../../../../view/phire/install'; if (isset($cfg['view'])) { $class = get_class($this); if (is_array($cfg['view']) && isset($cfg['view'][$class])) { $viewPath = $cfg['view'][$class]; } else { if (is_array($cfg['view']) && isset($cfg['view']['*'])) { $viewPath = $cfg['view']['*'] . '/install'; } else { if (is_string($cfg['view'])) { $viewPath = $cfg['view'] . '/install'; } } } } } $lang = isset($_GET['lang']) ? $_GET['lang'] : 'en_US'; if (!defined('POP_LANG')) { define('POP_LANG', $lang); } $this->i18n = I18n::factory(); $this->i18n->loadFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/vendor/Phire/data/assets/i18n/' . $this->i18n->getLanguage() . '.xml'); parent::__construct($request, $response, $project, $viewPath); $this->sess = Session::getInstance(); }
/** * Constructor method to instantiate the form object * * @param string $action * @param string $method * @return self */ public function __construct($action = null, $method = 'post') { $lang = isset($_GET['lang']) ? $_GET['lang'] : 'en_US'; if (!defined('POP_LANG')) { define('POP_LANG', $lang); } $this->i18n = I18n::factory(); $this->i18n->loadFile(__DIR__ . '/../../../data/assets/i18n/' . $this->i18n->getLanguage() . '.xml'); $this->initFieldsValues = $this->getInitFields(); parent::__construct($action, $method, null, ' '); $this->setAttributes('id', 'install-form'); }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; if (strpos($this->input, ' ') !== false) { $this->input = str_replace(' ', '', $this->input); } if (strpos($this->input, '-') !== false) { $this->input = str_replace('-', '', $this->input); } } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be a valid credit card number.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be a valid credit card number.'); } } // Evaluate the input against the validator $nums = str_split($this->input); $check = $nums[count($nums) - 1]; $start = count($nums) - 2; $sum = 0; $double = true; for ($i = $start; $i >= 0; $i--) { if ($double) { $num = $nums[$i] * 2; if ($num > 9) { $num = substr($num, 0, 1) + substr($num, 1, 1); } $sum += $num; $double = false; } else { $sum += $nums[$i]; $double = true; } } $sum += $check; $rem = $sum % 10; if (($rem == 0) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Install index controller and web config files prompt * * @return string */ public static function installWeb() { $msg = \Pop\I18n\I18n::factory()->__('Install index controller and web configuration files?') . ' ([A]pache/[I]IS/[O]ther/[N]o) '; echo $msg; $input = null; while ($input != 'a' && $input != 'i' && $input != 'o' && $input != 'n') { if (null !== $input) { echo $msg; } $prompt = fopen("php://stdin", "r"); $input = fgets($prompt, 32); $input = substr(strtolower(rtrim($input)), 0, 1); fclose($prompt); } return $input; }
/** * 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(); } }
/** * Install the controller class files * * @param \Pop\Config $install * @param string $installDir * @return void */ public static function install($install, $installDir) { echo \Pop\I18n\I18n::factory()->__('Creating controller class files...') . PHP_EOL; // Make the controller folder $module = substr($install->project->base, -1) == '/' ? 'module/' : '/module/'; $ctrlDir = $install->project->base . $module . $install->project->name . '/src/' . $install->project->name . '/Controller'; $viewDir = $install->project->base . $module . $install->project->name . '/view'; if (!file_exists($ctrlDir)) { mkdir($ctrlDir); } if (!file_exists($viewDir)) { mkdir($viewDir); } // Create the controller class files if (isset($install->controllers)) { $controllers = $install->controllers->asArray(); self::createControllers($controllers, array('src' => realpath($ctrlDir), 'view' => realpath($viewDir), 'namespace' => $install->project->name . '\\Controller', 'installDir' => $installDir)); } }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be included.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be included.'); } } // If input check is an array if (is_array($this->input)) { if (!is_array($this->value)) { $this->value = array($this->value); } $this->result = true; foreach ($this->value as $value) { if (in_array($value, $this->input) != $this->condition) { $this->result = false; } } // Else, if input check is a string } else { if (is_array($this->value)) { $this->result = in_array($this->input, $this->value) != $this->condition ? false : true; } else { if ((strpos((string) $this->input, (string) $this->value) !== false) == $this->condition) { $this->result = true; } else { $this->result = false; } } } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value format is not correct.'); } else { $this->defaultMessage = I18n::factory()->__('The value format is not correct.'); } } // Evaluate the input against the validator if (preg_match($this->value, $this->input) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be a valid email format.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be a valid email format.'); } } // Evaluate the input against the validator if (preg_match('/[a-zA-Z0-9\\.\\-\\_+%]+@[a-zA-Z0-9\\-\\_\\.]+\\.[a-zA-Z]{2,4}/', $this->input) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be greater than or equal to %1.', $this->value); } else { $this->defaultMessage = I18n::factory()->__('The value must not be greater than or equal to %1.', $this->value); } } // Evaluate the input against the validator if ($this->input >= $this->value == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must only contain characters of the alphabet.'); } else { $this->defaultMessage = I18n::factory()->__('The value must contain characters not in the alphabet.'); } } // Evaluate the input against the validator if (preg_match('/^[a-zA-Z]+$/', $this->input) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be a valid IPv4 subnet.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be a valid IPv4 subnet.'); } } // Evaluate the input against the validator if (preg_match('/^\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]|[0-9])\\b$/', $this->input) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be a valid IPv6 address.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be a valid IPv6 address.'); } } // Evaluate the input against the validator if (preg_match('/^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$/', $this->input) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value must be numeric.'); } else { $this->defaultMessage = I18n::factory()->__('The value must not be numeric.'); } } // Evaluate the input against the validator if ((is_numeric($this->input) == $this->value) != $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * Method to evaluate the validator * * @param mixed $input * @return boolean */ public function evaluate($input = null) { // Set the input, if passed if (null !== $input) { $this->input = $input; } $nums = explode('|', $this->value); // Set the default message if (null === $this->defaultMessage) { if ($this->condition) { $this->defaultMessage = I18n::factory()->__('The value length must be between or equal to %1 and %2.', $nums); } else { $this->defaultMessage = I18n::factory()->__('The value length must not be between or equal to %1 and %2.', $nums); } } // Evaluate the input against the validator if ((strlen($this->input) >= $nums[0] && strlen($this->input) <= $nums[1]) == $this->condition) { $this->result = true; } else { $this->result = false; } return $this->result; }
/** * 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(); } }
/** * Install the base folder and file structure * * @param \Pop\Config $install * @return void */ public static function install($install) { echo \Pop\I18n\I18n::factory()->__('Creating base folder and file structure...') . PHP_EOL; // Define folders to create $folders = array($install->project->base, $install->project->base . '/config', $install->project->base . '/module', $install->project->base . '/module/' . $install->project->name, $install->project->base . '/module/' . $install->project->name . '/config', $install->project->base . '/module/' . $install->project->name . '/data', $install->project->base . '/module/' . $install->project->name . '/src', $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name, $install->project->base . '/module/' . $install->project->name . '/view', $install->project->docroot); // Create the folders foreach ($folders as $folder) { if (!file_exists($folder)) { mkdir($folder); } } // Make the '/data' folder writable chmod($install->project->base . '/module/' . $install->project->name . '/data', 0777); // 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)) { $base = "__DIR__ . '/../'"; $docroot = "__DIR__ . '/../'"; // If the docroot is under the base } else { if (strlen($base) < strlen($docroot)) { $relDocroot = str_replace($base, '', $docroot); $base = "__DIR__ . '/../'"; $docroot = "__DIR__ . '/.." . $relDocroot . "'"; // If the base is under the docroot } else { if (strlen($base) > strlen($docroot)) { // Calculate how many levels up the docroot is from the base $diff = str_replace($docroot, '/', $base); $levels = substr_count($diff, '/'); $dirs = null; for ($i = 0; $i < $levels; $i++) { $dirs .= '../'; } $base = "__DIR__ . '/../'"; $docroot = "__DIR__ . '/" . $dirs . "'"; } } } // Create project.php file $projectCfg = new \Pop\Code\Generator($install->project->base . '/config/project.php'); $projectCfg->appendToBody('return new Pop\\Config(array(', true)->appendToBody(" 'base' => " . $base . ",")->appendToBody(" 'docroot' => " . $docroot, false); // Add the database config to it if (isset($install->databases)) { $projectCfg->appendToBody(",")->appendToBody(" 'databases' => array("); $databases = $install->databases->asArray(); $default = null; $i = 0; foreach ($databases as $dbname => $db) { $isPdo = stripos($db['type'], 'pdo') !== false ? true : false; $isSqlite = stripos($db['type'], 'sqlite') !== false ? true : false; if ($isPdo) { $pdoType = strtolower(substr($db['type'], strpos($db['type'], '_') + 1)); $realDbType = 'Pdo'; } else { $pdoType = null; $realDbType = $db['type']; } $projectCfg->appendToBody(" '" . $dbname . "' => Pop\\Db\\Db::factory('" . $realDbType . "', array ("); $j = 0; $default = $db['default'] ? $dbname : null; $dbCreds = $db; unset($dbCreds['type']); unset($dbCreds['prefix']); unset($dbCreds['default']); foreach ($dbCreds as $key => $value) { $j++; if ($isSqlite) { $dbFile = "__DIR__ . '/../module/" . $install->project->name . "/data/" . basename($value) . "'"; $ary = " '{$key}' => {$dbFile}"; } else { $ary = " '{$key}' => '{$value}'"; } if ($isPdo) { $ary .= "," . PHP_EOL . " 'type' => '{$pdoType}'"; } if ($j < count($dbCreds)) { $ary .= ','; } $projectCfg->appendToBody($ary); } $i++; $end = $i < count($databases) ? ' )),' : ' ))'; $projectCfg->appendToBody($end); } $projectCfg->appendToBody(' )', false); if (null !== $default) { $projectCfg->appendToBody("," . PHP_EOL . " 'defaultDb' => '" . $default . "'", false); } } // Save project config $projectCfg->appendToBody(PHP_EOL . '));', false); $projectCfg->save(); // Create the module config file $moduleCfg = new \Pop\Code\Generator($install->project->base . '/module/' . $install->project->name . '/config/module.php'); $moduleCfg->appendToBody('return array(')->appendToBody(" '{$install->project->name}' => new Pop\\Config(array(")->appendToBody(" 'base' => __DIR__ . '/../',")->appendToBody(" 'config' => __DIR__ . '/../config',")->appendToBody(" 'data' => __DIR__ . '/../data',")->appendToBody(" 'src' => __DIR__ . '/../src',")->appendToBody(" 'view' => __DIR__ . '/../view'")->appendToBody(" ))")->appendToBody(");", false)->save(); }
/** * Method to set the validator * * @throws \Pop\Form\Exception * @return void */ protected function setValidator() { // Get query data if ($_SERVER['REQUEST_METHOD']) { $queryData = array(); switch ($_SERVER['REQUEST_METHOD']) { case 'GET': $queryData = $_GET; break; case 'POST': $queryData = $_POST; break; default: $input = fopen('php://input', 'r'); $qData = null; while ($data = fread($input, 1024)) { $qData .= $data; } parse_str($qData, $queryData); } // If there is query data, set validator to check against the token value if (count($queryData) > 0) { $val = isset($queryData[$this->name]) ? $queryData[$this->name] : ''; $this->addValidator(new \Pop\Validator\Equal($val, \Pop\I18n\I18n::factory()->__('The security token does not match.'))); } } else { throw new \Pop\Form\Exception('Error: The server request method is not set.'); } }
/** * 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(); } }
/** * Return the two-letter language code from STDIN * * @param array $langs * @return string */ public static function getLanguage($langs) { $msg = I18n::factory()->__('Enter the numeric code for the default language: '); echo $msg; $lang = null; $keys = array_keys($langs); while (!array_key_exists($lang, $keys)) { if (null !== $lang) { echo $msg; } $prompt = fopen("php://stdin", "r"); $lang = fgets($prompt, 5); $lang = rtrim($lang); fclose($prompt); } return $lang; }
/** * Method to calculate the elapsed time between the date passed and now. * * @param string $time * @return string */ public static function calculateTime($time) { // Calculate the difference. $timeDiff = time() - strtotime($time); $timePhrase = null; // If less than an hour. if ($timeDiff < 3600) { $elapsedTime = round($timeDiff / 60); if ($elapsedTime <= 0) { $timePhrase = I18n::factory()->__('A few seconds ago'); } else { if ($elapsedTime == 1) { $timePhrase = I18n::factory()->__('1 minute ago'); } else { $timePhrase = I18n::factory()->__('%1 minutes ago', $elapsedTime); } } // If less than a day. } else { if ($timeDiff >= 3600 && $timeDiff < 86400) { $elapsedTime = round($timeDiff / 60 / 60); $timePhrase = $elapsedTime == 1 ? I18n::factory()->__('1 hour ago') : I18n::factory()->__('%1 hours ago', $elapsedTime); // If less than a month. } else { if ($timeDiff >= 86400 && $timeDiff < 2592000) { $elapsedTime = round($timeDiff / 60 / 60 / 24); $timePhrase = $elapsedTime == 1 ? I18n::factory()->__('1 day ago') : I18n::factory()->__('%1 days ago', $elapsedTime); // If more than a month, less than 2 years } else { if ($timeDiff >= 2592000 && $timeDiff < 63072000) { $elapsedTime = round($timeDiff / 60 / 60 / 24 / 30); $timePhrase = $elapsedTime == 1 ? I18n::factory()->__('1 month ago') : I18n::factory()->__('%1 months ago', $elapsedTime); // If more than 2 years ago } else { $elapsedTime = round($timeDiff / 60 / 60 / 24 / 30 / 12); $timePhrase = I18n::factory()->__('%1 years ago', $elapsedTime); } } } } // Return the calculated elapsed time. return $timePhrase; }
/** * Return the input from STDIN * * @param string $msg * @return string */ protected static function cliInput($msg = null) { echo null === $msg ? \Pop\I18n\I18n::factory()->__('Continue?') . ' (Y/N) ' : $msg; $input = null; while (null === $input) { $prompt = fopen("php://stdin", "r"); $input = fgets($prompt); $input = rtrim($input); fclose($prompt); } return $input; }
/** * Static method to get i18n object * * @return \Pop\I18n\I18n */ public static function getI18n() { $lang = static::findById('default_language')->value; if (!defined('POP_LANG')) { define('POP_LANG', $lang); $i18n = \Pop\I18n\I18n::factory($lang); } else { $i18n = \Pop\I18n\I18n::factory(POP_LANG); } $i18n->loadFile(__DIR__ . '/../../../data/assets/i18n/' . $i18n->getLanguage() . '.xml'); // Load any module language files $modules = Extensions::findAll(null, array('type' => 1)); foreach ($modules->rows as $module) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/' . $module->name) && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/' . $module->name . '/data') && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/' . $module->name . '/data/assets/i18n') && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/' . $module->name . '/data/assets/i18n/' . $i18n->getLanguage() . '.xml')) { $i18n->loadFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/' . $module->name . '/data/assets/i18n/' . $i18n->getLanguage() . '.xml'); } } return $i18n; }
/** * Validate the form element object. * * @return boolean */ public function validate() { $this->errors = array(); // Check if the element is required. if ($this->required == true) { if (is_array($this->value)) { $curElemValue = $this->marked; } else { if ($_FILES && isset($_FILES[$this->name]['name'])) { $curElemValue = $_FILES[$this->name]['name']; } else { $curElemValue = $this->value; } } if (empty($curElemValue) && $curElemValue != '0') { $this->errors[] = \Pop\I18n\I18n::factory()->__('This field is required.'); } } // Check the element's validators. if (isset($this->validators[0])) { foreach ($this->validators as $validator) { $curElemSize = null; if (is_array($this->value)) { $curElemValue = $this->marked; } else { if ($_FILES && isset($_FILES[$this->name]['name'])) { $curElemValue = $_FILES[$this->name]['name']; $curElemSize = $_FILES[$this->name]['size']; } else { $curElemValue = $this->value; } } // If Pop\Validator\* if ($validator instanceof \Pop\Validator\ValidatorInterface) { if ('Pop\\Validator\\NotEmpty' == get_class($validator)) { if (!$validator->evaluate($curElemValue)) { $this->errors[] = $validator->getMessage(); } } else { if (null !== $curElemSize && 'Pop\\Validator\\LessThanEqual' == get_class($validator)) { if (!$validator->evaluate($curElemSize)) { $this->errors[] = $validator->getMessage(); } } else { if (!empty($curElemValue) && !$validator->evaluate($curElemValue)) { $this->errors[] = $validator->getMessage(); } } } // Else, if callable } else { $result = call_user_func_array($validator, array($curElemValue)); if (null !== $result) { $this->errors[] = $result; } } } } // If errors are found on any of the form elements, return false. return count($this->errors) > 0 ? false : true; }
public function testLoadFileBadFileException() { $this->setExpectedException('Exception'); $l = I18n::factory(); $l->loadFile(__DIR__ . '/../tmp/access.txt'); }
/** * Method to set the validator * * @throws \Pop\Form\Exception * @return void */ protected function setValidator() { // Get query data if ($_SERVER['REQUEST_METHOD']) { $queryData = array(); switch ($_SERVER['REQUEST_METHOD']) { case 'GET': $queryData = $_GET; break; case 'POST': $queryData = $_POST; break; default: $input = fopen('php://input', 'r'); $qData = null; while ($data = fread($input, 1024)) { $qData .= $data; } parse_str($qData, $queryData); } // If there is query data, set validator to check against the token value if (count($queryData) > 0) { if (isset($queryData[$this->name])) { $captcha = $this->token['captcha']; if (stripos($captcha, '<img') !== false) { $answer = $this->token['value']; } else { if (strpos($captcha, '<img') === false && (strpos($captcha, ' + ') !== false || strpos($captcha, ' - ') !== false || strpos($captcha, ' * ') !== false || strpos($captcha, ' / ') !== false)) { $answer = eval("return ({$captcha});"); } else { $answer = $captcha; } } $this->addValidator(new \Pop\Validator\Equal($answer, \Pop\I18n\I18n::factory()->__('The answer is incorrect.'))); } } } else { throw new \Pop\Form\Exception('Error: The server request method is not set.'); } }
/** * Method to get the authentication result message * * @return string */ public function getResultMessage() { $msg = null; switch ($this->result) { case self::USER_IS_VALID: $msg = I18n::factory()->__('The user is valid.'); break; case self::USER_NOT_FOUND: $msg = I18n::factory()->__('The user was not found.'); break; case self::USER_IS_BLOCKED: $msg = I18n::factory()->__('The user is blocked.'); break; case self::PASSWORD_INCORRECT: $msg = I18n::factory()->__('The password was incorrect.'); break; case self::ATTEMPTS_EXCEEDED: $msg = I18n::factory()->__('The allowed login attempts (%1) have been exceeded.', $this->validators['attempts']->getValue()); break; case self::IP_BLOCKED: $msg = I18n::factory()->__('That IP address is blocked.'); break; case self::IP_NOT_ALLOWED: $msg = I18n::factory()->__('That IP address is not allowed.'); break; } return $msg; }