Esempio n. 1
0
 /**
  * Writes the post to the disk and updates the metadata store
  *  By default, posts stay in "draft" status.
  *  If $status is "published", and $publish is null, current datetime is used
  *
  * @param $type
  * @param $postInfo
  * @param string $status
  * @param null $publish
  */
 public function addPost($type, $postInfo, $status = 'draft', $publish = null)
 {
     if ($this->rootFolder != '') {
         // make sure that required content folders exist
         if (!file_exists($this->rootFolder . '/contents')) {
             mkdir($this->rootFolder . '/contents');
         }
         if (!file_exists($this->rootFolder . '/contents/' . $type)) {
             mkdir($this->rootFolder . '/contents/' . $type);
         }
         // add more fields automatically
         $id = $this->getNewID();
         $postInfo['id'] = $id;
         $postInfo['type'] = $type;
         $postInfo['status'] = $status;
         $postInfo['created'] = \Temple\DateTimeUtil::now()->format('Y-m-d H:i:s');
         $postInfo['creator'] = $this->currentUser;
         if ($status == 'published') {
             $postInfo['published'] = !is_null($publish) ? $publish : $postInfo['created'];
         } else {
             $postInfo['published'] = '';
         }
         // write out the JSON file
         $postFilePath = $this->rootFolder . '/contents/' . $type . '/' . $id . '.json';
         file_put_contents($postFilePath, json_encode($postInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
         // refresh metadata on the disk
         $this->refreshMetadata($postInfo);
     }
 }
Esempio n. 2
0
 /**
  * Sends mail to external email address. wraps standard php function with useful options:
  *   'intercept': expects an email address to send.
  *      All MAMP mails are intercepted by default.
  *      To prevent intercepting on local machine, Runtime context can be used (explicitly set 'intercept' context item to false)
  *   'format': expects either 'text' or 'html' ('html' is used by default)
  *
  * @param $to
  * @param $subj
  * @param $body
  * @param array $options
  *
  * @return bool
  */
 public static function mail($to, $subj, $body, $options = array())
 {
     $config = Config::getInstance();
     $env = $config->get('ENV');
     // Intercept message
     if (in_array($env, array('MAMP', 'TEST')) && Runtime::getInstance()->getContextItem('intercept', true)) {
         // on MAMP, intercept every outgoing message, if not explicitly disabled in Runtime context
         // on TEST, add admin email(s) to original address, if not explicitly disabled in Runtime context
         $intercept = Util::lavnn('adminEmail', $config->getEnvConfig(), $config->get('adminEmail'));
         if ($env == 'TEST') {
             $intercept = $to . ',' . $intercept;
         }
         $subj = '[' . $env . '] ' . $subj;
     } else {
         $intercept = Util::lavnn('intercept', $options, '');
     }
     // generate a unique hash for the mail
     $now = DateTimeUtil::fixTime();
     $toAsString = is_array($to) ? join(',', $to) : $to;
     $hash = md5($toAsString . ':' . $now . ':' . $subj);
     if ($intercept != '') {
         //$to = explode(',', $intercept);
         $to = $intercept;
     }
     // set the transport option
     if (!isset($options['transport'])) {
         $options['transport'] = $config->getEnvSetting('mailTransport');
     }
     $result = false;
     $intro = Util::lavnn('intro', $options, '');
     if ($to != '') {
         $result = MailUtil::transportMail($to, $subj, $body, $hash, $options);
         // if first sending attempt to provided transport failed, try alternatives (if any)
         if (!$result && isset($options['alternativeTransport'])) {
             foreach ((array) $options['alternativeTransport'] as $alternativeTransport) {
                 $options['transport'] = $alternativeTransport;
                 if ($to != '') {
                     $result = MailUtil::transportMail($to, $subj, $body, $hash, $options);
                     if ($result) {
                         break;
                     }
                 }
             }
         }
     } else {
         $messageParams = array('email' => $to, 'subject' => $subj, 'message' => $body, 'intro' => $intro);
         $adminAlert = new AdminAlert();
         $adminAlert->insert(array('title' => 'Message sent with empty email address', 'description' => print_r($messageParams, 1), 'context' => print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1)));
     }
     if ($result) {
         // save sent email to the database
         $mailModel = new MailMessageSent();
         $mailParams = array('hash' => $hash, 'email' => is_array($to) ? join(', ', $to) : $to, 'subject' => $subj, 'message' => $body, 'intro' => $intro, 'sent' => $now);
         $manual = Util::lavnn('manual', $options, 0);
         if ($manual > 0) {
             $mailParams['manual'] = 1;
         }
         $sender = Util::lavnn('sender', $options, 0);
         if ($sender > 0) {
             $mailParams['sender'] = $sender;
         }
         $mailParams['signature'] = Util::lavnn('signature', $options, 'team');
         $mailParams['language'] = Util::lavnn('language', $options, 'en');
         // @TODO use from $config
         $result = $mailModel->insert($mailParams) > 0;
     }
     return $result;
 }