Exemple #1
0
 public function createApp()
 {
     $this->createFolderIfNotExists('/app');
     $this->createFolderIfNotExists('/app/cache');
     $this->createFolderIfNotExists('/app/modules');
     $this->createFolderIfNotExists('/app/modules.translation');
     $this->copyFileIfNotExists('/app/autoload.php', 'autoload.php');
     $replacements = $this->config;
     $replacements['APP_NAME'] = $this->getAppName();
     $namespace = Util::lavnn('APP_NAMESPACE', $replacements, '');
     $replacements['APP_NAMESPACE'] = $this->prompt('Project namespace?', $namespace);
     $description = Util::lavnn('DESCRIPTION', $replacements, '');
     $replacements['DESCRIPTION'] = $this->prompt('Project description?', $description);
     $description = Util::lavnn('WEBSITE', $replacements, '');
     $replacements['WEBSITE'] = $this->prompt('Website URL?', $description);
     $description = Util::lavnn('LICENSE', $replacements, '');
     $replacements['LICENSE'] = $this->prompt('License?', $description);
     $description = Util::lavnn('AUTHOR_NAME', $replacements, '');
     $replacements['AUTHOR_NAME'] = $this->prompt('Author name?', $description);
     $description = Util::lavnn('AUTHOR_EMAIL', $replacements, '');
     $replacements['AUTHOR_EMAIL'] = $this->prompt('Author email?', $description);
     $this->copyFileIfNotExists('/composer.json', 'composer.json', $replacements);
     print 'Create modules? (Y/n)';
     $answer = strtoupper(trim(fgets($this->fp)));
     if ($answer == 'Y' || $answer == '') {
         do {
             print 'module name (provide no name to quit): ';
             $moduleName = trim(fgets($this->fp));
             if ($moduleName != '') {
                 $this->createModule($moduleName);
             }
             print PHP_EOL;
         } while ($moduleName != '');
     }
 }
Exemple #2
0
 /**
  * Renders page flash message (informational or error) and cleans up session
  *
  * @return string
  */
 public function renderFlashMessage()
 {
     $flash = Util::lavnn('flash', $_SESSION, '');
     $error = Util::lavnn('error', $_SESSION, '');
     $output = '';
     if ($flash != '') {
         $output .= TextProcessor::doTemplate('framework', '_flash', array('flash' => $flash));
         unset($_SESSION['flash']);
     }
     if ($error != '') {
         $output .= TextProcessor::doTemplate('framework', '_error', array('error' => $error));
         unset($_SESSION['error']);
     }
     return $output;
 }
 public function send($transports = array())
 {
     $output = false;
     $queuedMessageInfo = $this->getData();
     try {
         $options = array('intro' => Util::lavnn('intro', $queuedMessageInfo, ''), 'manual' => Util::lavnn('manual', $queuedMessageInfo, 0), 'signature' => Util::lavnn('signature', $queuedMessageInfo, 'team'), 'language' => Util::lavnn('language', $queuedMessageInfo, 'en'), 'sender' => Util::lavnn('sender', $queuedMessageInfo, 0));
         if (count($transports) > 0) {
             $options['transport'] = $transports[0];
         }
         if (count($transports) > 1) {
             $options['alternativeTransport'] = $transports[1];
         }
         $output = MailUtil::mail($queuedMessageInfo['email'], $queuedMessageInfo['subject'], $queuedMessageInfo['message'], $options);
     } catch (\Exception $ex) {
         $env = Runtime::getInstance()->config['ENV'];
         $adminAlert = new AdminAlert();
         $adminAlert->insert(array('title' => '[' . $env . '] Failed email sending attempt to address "' . $queuedMessageInfo['email'], 'description' => print_r($queuedMessageInfo, 1), 'context' => print_r($ex->getTrace(), 1)));
     }
     return $output;
 }
Exemple #4
0
 public static function setDictionary($dictionaryName, $dictionary)
 {
     /** @var $instance Cache */
     $instance = self::getInstance();
     if (Util::lavnn('dictionaries', $instance->settings, false)) {
         $instance->dictionaries[$dictionaryName] = $dictionary;
         self::set('dictionary/' . $dictionaryName, json_encode($dictionary, JSON_UNESCAPED_UNICODE + JSON_PRETTY_PRINT));
     }
 }
Exemple #5
0
 public function getFolderPath($name)
 {
     return Util::lavnn($name, $this->folders, '');
 }
Exemple #6
0
 public function loadModules()
 {
     if ($modulesConfig = Cache::get('settings:modulesConfig')) {
         $this->config['modules'] = json_decode($modulesConfig, true);
     } else {
         // $config variable can be modified withing both shared and app modules.
         //  we store the configurations by module names, thus enabling overriding
         foreach (glob($this->config['folders']['shared'] . 'modules/*') as $module) {
             if (file_exists("{$module}/settings/config.php")) {
                 $config = array();
                 include "{$module}/settings/config.php";
                 $this->config['modules'][basename($module)] = $config;
             }
         }
         // app modules are overwriting settings from shared modules
         foreach (glob($this->config['folders']['app'] . 'modules/*') as $module) {
             $moduleName = basename($module);
             if (file_exists("{$module}/settings/config.php")) {
                 $config = Util::lavnn($moduleName, $this->config['modules'], array());
                 include "{$module}/settings/config.php";
                 // app modules can override the settings
                 $this->config['modules'][$moduleName] = $config;
             }
         }
         Cache::set('settings:modulesConfig', json_encode($this->config['modules'], JSON_UNESCAPED_UNICODE));
     }
 }
Exemple #7
0
 /**
  * Routes HTTP request to proper action file
  *
  * @param array $request
  */
 public function route($request)
 {
     $f = Util::lavnn('f', $request, '');
     $i = Util::lavnn('i', $request, '');
     $json = Util::lavnn('json', $request, '');
     $pdf = Util::lavnn('pdf', $request, '');
     $p = Util::lavnn('p', $request, '');
     $cron = Util::lavnn('cron', $request, '');
     if ($f != '' && ($fileName = $this->checkAction($f))) {
         $this->routeFormAction($f, $fileName);
         exit;
     } elseif ($i != '' && ($fileName = $this->checkAction($i))) {
         $this->routeInlineAction($i, $fileName);
         exit;
     } elseif ($json != '') {
         // JSON is a special case. We need to ensure that output is at least empty array.
         //  Therefore, we cannot rely on default behaviour if request is not routed.
         //  So, we still serve some output if action is not found.
         //  Consumer is then responsible to check the structure of returned JSON
         $output = array();
         $this->routeJsonAction($json);
         print json_encode($output);
         exit;
     } elseif ($pdf != '' && ($fileName = $this->checkAction($pdf))) {
         $this->routePdfAction($pdf, $fileName);
         exit;
     } elseif ($p != '' && ($fileName = $this->checkAction($p))) {
         $this->routePageAction($p, $fileName);
         exit;
     } elseif ($cron != '' && ($fileName = $this->checkAction($cron))) {
         $this->routeCronAction($cron, $fileName);
         exit;
     }
     // Nothing was matched!
     HttpResponse::dynamic404();
 }
Exemple #8
0
 /**
  * Returns result of select query in one of supported format
  *
  * @param array $searchParameters
  * @param array $options
  *
  * @return array
  */
 public function select($searchParameters = array(), $options = array())
 {
     $format = Util::lavnn('format', $options, 'array');
     $sorting = Util::lavnn('sort', $options, '');
     $sqlFileName = get_class($this) . '_Search';
     if (Locator::moduleFileExists($this->moduleName, "sql/{$sqlFileName}.sql")) {
         // We can override default search behaviour with custom SQL
         $query = TextProcessor::doSqlTemplate($this->moduleName, $sqlFileName, $searchParameters);
     } else {
         $where = $this->prepareWhere($searchParameters, $this->getMetadata(), __METHOD__);
         $sqlParams = array('tableName' => $this->tableName, 'whereClause' => count($where) ? 'WHERE ' . join(' AND ', $where) : '', 'orderClause' => $sorting != '' ? 'ORDER BY ' . $sorting : '');
         $query = TextProcessor::doText(file_get_contents(__DIR__ . "/Templates/Search.sql"), $sqlParams);
     }
     return $this->selectByQuery($query, $format);
 }
Exemple #9
0
 public function generateCaptchaImage($captchaText, $imageFileName, $options)
 {
     //region parse settings from $options or use default values
     $width = Util::lavnn('width', $options, 180);
     // width of resulting captcha image
     $height = Util::lavnn('height', $options, 100);
     // height of resulting captcha image
     $paletteSize = Util::lavnn('paletteSize', $options, 7);
     // from how many random colors to choose
     $linePosition = mt_rand(30, 80);
     // the top margin for a text
     $letterPosition = mt_rand(10, 50);
     // coordinate of each rendered letter
     $fontFile = $this->r->config['folders']['fonts'] . Util::lavnn('font', $options, 'DejaVuSansMono') . '.ttf';
     //endregion
     $captcha = imagecreate($width, $height);
     $backgroundColor = ImageColorAllocate($captcha, 255, 255, 255);
     //region Allocate colors, providing 150 as max value (colors should not be too close to white)
     $palette = array();
     for ($i = 0; $i < $paletteSize; $i++) {
         $palette[] = ImageColorAllocate($captcha, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
     }
     //endregion
     //region Add text that user should read
     foreach (mb_str_split($captchaText) as $letterCharacter) {
         $letterAngle = mt_rand(-18, 18);
         // each letter can be randomly inclined a little bit
         $letterSize = mt_rand(16, 28);
         // each letter can be of slightly different size
         $letterImageInfo = imagettftext($captcha, $letterSize, $letterAngle, $letterPosition, $linePosition, $palette[mt_rand(0, $paletteSize - 1)], $fontFile, $letterCharacter);
         $topRightX = $letterImageInfo[2];
         $bottomRightX = $letterImageInfo[4];
         // Calculate position for the next character
         $letterPosition = max($topRightX, $bottomRightX) + 5;
     }
     //endregion
     //region Add random lines and other graphical noise
     for ($i = 0; $i < $width * $height * 0.001; $i++) {
         imagesetthickness($captcha, 1);
         imageline($captcha, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $palette[mt_rand(0, $paletteSize - 1)]);
     }
     for ($i = 0; $i < $width * $height * mt_rand(5, 10) / 100; $i++) {
         imagesetpixel($captcha, mt_rand(1, $width), mt_rand(1, $height), $palette[mt_rand(0, $paletteSize - 1)]);
     }
     //endregion
     header('Content-Type: image/png');
     imagepng($captcha, $imageFileName);
     imagedestroy($captcha);
 }
Exemple #10
0
 public static function removeQueryLimits($query)
 {
     $parts = Util::explode('LIMIT', $query, 2);
     return str_replace('SQL_CALC_FOUND_ROWS', '', $parts[0]);
 }
Exemple #11
0
 /**
  * Transport mail message with PhpMailer library
  *
  * @param $to
  * @param $subj
  * @param $body
  * @param $hash
  * @param $intro
  * @param $options
  *
  * @return bool
  */
 public static function transportMailPhpMailer($to, $subj, $body, $hash, $intro, $options)
 {
     if (!is_array($to)) {
         $to = explode(',', $to);
     }
     $signature = Util::lavnn('signature', $options, 'team');
     $language = Util::lavnn('language', $options, 'en');
     $sender = Util::lavnn('sender', $options, 0);
     $config = Config::getInstance();
     $envConfig = $config->getEnvConfig();
     $mail = new \PHPMailer();
     $mail->isSMTP();
     // Set mailer to use SMTP
     $mail->Host = $config->get('SMTP_HOST');
     // Specify main and backup SMTP servers
     $mail->SMTPAuth = true;
     // Enable SMTP authentication
     $mail->Username = $config->get('SMTP_USER');
     // SMTP username
     $mail->Password = $config->get('SMTP_PASSWORD');
     // SMTP password
     $mail->SMTPSecure = 'tls';
     // Enable encryption, 'ssl' also accepted
     $mail->From = Util::lavnn('mailFrom', $envConfig, '*****@*****.**');
     $mail->FromName = Util::lavnn('mailFromName', $envConfig, 'Info@Tilpy');
     foreach ($to as $email) {
         $mail->addAddress($email);
     }
     $mail->addReplyTo($mail->From, $mail->FromName);
     /*
     $mail->addBCC('*****@*****.**');
     $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
     $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
     $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
     */
     $mail->isHTML(true);
     // Set email format to HTML
     $mail->Subject = $subj;
     $mail->Body = self::prepareHtmlMail($subj, $body, $hash, $intro, $signature, $language, $sender);
     $mail->AltBody = $body = self::prepareTextMail($subj, $body, $hash, $intro, $signature, $language, $sender);
     $result = $mail->send();
     if (!$result) {
         // @TODO decide what to do with mail sending errors
         echo 'Message could not be sent.';
         echo 'Mailer Error: ' . $mail->ErrorInfo;
     }
     return $result;
 }
Exemple #12
0
 /**
  * @param Model $model
  */
 public function setModelData($model)
 {
     $data = $model->getExtendedData();
     $tableName = $model->getTableName();
     $id = Util::lavnn('id', $data, 0);
     if ($id > 0) {
         // save the value in cache
         $key = 'models/' . $tableName . '/' . $id;
         $this->set($key, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
         // also save in memory
         $this->models[$tableName][$id] = $data;
     }
 }