public function prepareView(&$comments)
 {
     $email = wa_make_pattern(trim($this->getSettingValue('email')));
     if ($email) {
         $pattern = '/(.*' . preg_replace('/[,\\n\\s]{1,}/', '|.*', $email) . ')/i';
         if (wa()->getEnv() == 'backend' && wa()->getUser()->isAdmin($this->app_id)) {
             $label = '<a href="?module=plugins&amp;slug=troll"><i class="icon16 troll"  title="' . ($title = _wp('Troll')) . '"><!-- trollface --></i></a>';
         } else {
             $label = '<i class="icon16 troll"  title="' . ($title = _wp('Troll!')) . '"><!-- trollface --></i>';
         }
         foreach ($comments as &$comment) {
             $email = false;
             if (!$comment['contact_id'] && ($comment['email'] && preg_match($pattern, $comment['email']) || $comment['site'] && preg_match($pattern, $comment['site']) || $comment['name'] && preg_match($pattern, $comment['name']))) {
                 $comment['plugins']['authorname_suffix'][$this->id] = $label;
             }
             unset($comment);
         }
     }
 }
 public function prepareView(&$comments)
 {
     $email = wa_make_pattern(trim($this->getSettingValue('email')));
     if ($email) {
         $pattern = '/(.*' . preg_replace('/[,\\n\\s]{1,}/', '|.*', $email) . ')/i';
         if (wa()->getEnv() == 'backend' && wa()->getUser()->isAdmin($this->app_id)) {
             $label = '<a href="?module=plugins&amp;slug=troll"><i class="icon16 troll"  title="' . ($title = _wp('Troll')) . '"><!-- trollface --></i></a>';
         } else {
             $label = '<i class="icon16 troll"  title="' . ($title = _wp('Troll!')) . '"><!-- trollface --></i>';
         }
         // Fetch emails of registered users
         $contact_troll = array();
         $check_emails = array();
         foreach ($comments as $comment) {
             if (!empty($comment['contact_id'])) {
                 $contact_troll[$comment['contact_id']] = preg_match($pattern, $comment['name']);
                 if (!$contact_troll[$comment['contact_id']]) {
                     $check_emails[$comment['contact_id']] = 1;
                 }
             }
         }
         $contact_model = new waContactEmailsModel();
         foreach ($contact_model->getByField('contact_id', array_keys($check_emails), true) as $row) {
             if (empty($contact_troll[$row['contact_id']]) && preg_match($pattern, $row['email'])) {
                 $contact_troll[$row['contact_id']] = true;
             }
         }
         foreach ($comments as &$comment) {
             if (!empty($comment['contact_id'])) {
                 if (!empty($contact_troll[$comment['contact_id']])) {
                     $comment['plugins']['authorname_suffix'][$this->id] = $label;
                 }
             } else {
                 if ($comment['email'] && preg_match($pattern, $comment['email']) || $comment['site'] && preg_match($pattern, $comment['site']) || $comment['name'] && preg_match($pattern, $comment['name'])) {
                     $comment['plugins']['authorname_suffix'][$this->id] = $label;
                 }
             }
             unset($comment);
         }
     }
 }
 /**
  *
  * Extract theme from archive
  * @throws Exception
  * @param string $source_path archive path
  *
  * @return waTheme
  */
 public static function extract($source_path)
 {
     static $white_list = array('js', 'css', 'html', 'txt', 'png', 'jpg', 'jpeg', 'jpe', 'tiff', 'bmp', 'gif', 'svg', 'htc', 'cur', 'ttf', 'eot', 'otf', 'woff', '');
     $autoload = waAutoload::getInstance();
     $autoload->add('Archive_Tar', 'wa-installer/lib/vendors/PEAR/Tar.php');
     $autoload->add('PEAR', 'wa-installer/lib/vendors/PEAR/PEAR.php');
     $instance = null;
     if (class_exists('Archive_Tar')) {
         try {
             $tar_object = new Archive_Tar($source_path, true);
             $files = $tar_object->listContent();
             if (!$files) {
                 self::throwArchiveException('INVALID_OR_EMPTY_ARCHIVE');
             }
             //search theme info
             $info = false;
             $pattern = "@(/|^)" . wa_make_pattern(self::PATH, '@') . "\$@";
             foreach ($files as $file) {
                 if (preg_match($pattern, $file['filename'])) {
                     $info = $tar_object->extractInString($file['filename']);
                     break;
                 }
             }
             if (!$info) {
                 self::throwThemeException('MISSING_THEME_XML');
             }
             $xml = @simplexml_load_string($info);
             $app_id = (string) $xml['app'];
             $id = (string) $xml['id'];
             if (!$app_id) {
                 self::throwThemeException('MISSING_APP_ID');
             } elseif (!$id) {
                 self::throwThemeException('MISSING_THEME_ID');
             } else {
                 if ($app_info = wa()->getAppInfo($app_id)) {
                     //TODO check theme support
                     if ($parent_theme = (string) $xml['parent_theme_id']) {
                         $parent_theme = explode(':', $parent_theme, 2);
                         try {
                             if (count($parent_theme) == 2) {
                                 new waTheme($parent_theme[1], $parent_theme[0]);
                             } else {
                                 new waTheme($parent_theme[0], $app_id);
                             }
                         } catch (Exception $ex) {
                             self::throwThemeException('PARENT_THEME_NOT_FOUND', $ex->getMessage());
                         }
                     }
                 } else {
                     $message = sprintf(_w('Theme “%s” is for app “%s”, which is not installed in your Webasyst. Install the app, and upload theme once again.'), $id, $app_id);
                     throw new waException($message);
                 }
             }
             $wa_path = "wa-apps/{$app_id}/themes/{$id}";
             $wa_pattern = wa_make_pattern($wa_path, '@');
             $file = reset($files);
             if (preg_match("@^{$wa_pattern}(/|\$)@", $file['filename'])) {
                 $extract_path = $wa_path;
                 $extract_pattern = $wa_pattern;
             } else {
                 $extract_path = $id;
                 $extract_pattern = wa_make_pattern($id, '@');
                 if (!preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     $extract_path = '';
                     $extract_pattern = false;
                 }
             }
             if ($extract_path) {
                 $extract_path = trim($extract_path, '/') . '/';
             }
             $missed_files = array();
             foreach ($xml->xpath('/theme/files/file') as $theme_file) {
                 $path = (string) $theme_file['path'];
                 $parent = intval((string) $theme_file['parent']);
                 if (!in_array(pathinfo($theme_file['path'], PATHINFO_EXTENSION), array('html', 'js', 'css'))) {
                     self::throwThemeException('UNEXPECTED_EDITABLE_FILE_TYPE', $theme_file['path']);
                 }
                 if (!$parent) {
                     $missed_files[$path] = $extract_path . $path;
                 }
             }
             #angry check
             foreach ($files as $file) {
                 if ($extract_pattern && !preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     self::throwThemeException('UNEXPECTED_FILE_PATH', "{$file['filename']}. Expect files in [{$extract_path}] directory");
                 } elseif (preg_match('@\\.(php\\d*|pl)@', $file['filename'], $matches)) {
                     if (preg_match('@(^|/)build\\.php$@', $file['filename'])) {
                         $file['content'] = $tar_object->extractInString($file['filename']);
                         if (!preg_match('@^<\\?php[\\s\\n]+return[\\s\\n]+\\d+;[\\s\\n]*$@', $file['content'])) {
                             self::throwThemeException('UNEXPECTED_FILE_CONTENT', $file['filename']);
                         }
                     } else {
                         self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                     }
                 } else {
                     if (preg_match('@(^|/)\\.htaccess$@', $file['filename'])) {
                         $file['content'] = $tar_object->extractInString($file['filename']);
                         if (preg_match('@\\b(add|set)Handler\\b@ui', $file['content'])) {
                             self::throwThemeException('INVALID_HTACCESS', $file['filename']);
                         }
                     } elseif (!in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), $white_list)) {
                         if (!in_array(strtolower(basename($file['filename'])), array('theme.xml', 'build.php', '.htaccess', 'readme'))) {
                             self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                         }
                     }
                     if ($extract_pattern) {
                         $file['filename'] = preg_replace("@^{$extract_pattern}/?@", '', $file['filename']);
                     }
                     if (empty($file['typeflag']) && !empty($file['filename']) && isset($missed_files[$file['filename']])) {
                         unset($missed_files[$file['filename']]);
                     }
                 }
             }
             if (!empty($missed_files)) {
                 self::throwThemeException('MISSING_DESCRIBED_FILES', implode(', ', $missed_files));
             }
             self::verify($id);
             self::protect($app_id);
             $target_path = wa()->getDataPath("themes/{$id}", true, $app_id, false);
             waFiles::delete($target_path);
             if ($extract_path && !$tar_object->extractModify($target_path, $extract_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             } elseif (!$tar_object->extract($target_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             }
             $instance = new self($id, $app_id);
             $instance->check();
         } catch (Exception $ex) {
             if (isset($target_path) && $target_path) {
                 waFiles::delete($target_path, true);
             }
             throw $ex;
         }
     } else {
         self::throwArchiveException('UNSUPPORTED_ARCHIVE_TYPE');
     }
     return $instance;
 }
 /**
  *
  * Extract theme from archive
  * @throws Exception
  * @param string $source_path archive path
  *
  * @return waTheme
  */
 public static function extract($source_path)
 {
     $autoload = waAutoload::getInstance();
     $autoload->add('Archive_Tar', 'wa-installer/lib/vendors/PEAR/Tar.php');
     $autoload->add('PEAR', 'wa-installer/lib/vendors/PEAR/PEAR.php');
     if (class_exists('Archive_Tar')) {
         try {
             $tar_object = new Archive_Tar($source_path, true);
             $files = $tar_object->listContent();
             if (!$files) {
                 self::throwArchiveException('INVALID_OR_EMPTY_ARCHIVE');
             }
             //search theme info
             $theme_check_files = array(self::PATH);
             $theme_files_map = array();
             $info = false;
             $pattern = "/(\\/|^)" . wa_make_pattern(self::PATH) . "\$/";
             foreach ($files as $file) {
                 if (preg_match($pattern, $file['filename'])) {
                     $info = $tar_object->extractInString($file['filename']);
                     break;
                 }
             }
             if (!$info) {
                 self::throwThemeException('MISSING_THEME_XML');
             }
             $xml = @simplexml_load_string($info);
             $app_id = (string) $xml['app'];
             $id = (string) $xml['id'];
             if (!$app_id) {
                 self::throwThemeException('MISSING_APP_ID');
             } elseif (!$id) {
                 self::throwThemeException('MISSING_THEME_ID');
             } else {
                 if ($app_info = wa()->getAppInfo($app_id)) {
                     //TODO check theme support
                 } else {
                     $message = sprintf(_w('Theme “%s” is for app “%s”, which is not installed in your Webasyst. Install the app, and upload theme once again.'), $id, $app_id);
                     throw new waException($message);
                 }
             }
             $wa_path = "wa-apps/{$app_id}/themes/{$id}";
             $wa_pattern = wa_make_pattern($wa_path);
             $file = reset($files);
             if (preg_match("@^{$wa_pattern}(/|\$)@", $file['filename'])) {
                 $extract_path = $wa_path;
                 $extract_pattern = $wa_pattern;
             } else {
                 $extract_path = $id;
                 $extract_pattern = wa_make_pattern($id);
                 if (!preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     $extract_path = '';
                     $extract_pattern = false;
                 }
             }
             foreach ($files as $file) {
                 if ($extract_pattern && !preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     self::throwThemeException('UNEXPECTED_FILE_PATH', "{$file['filename']}. Expect files in [{$extract_path}] directory");
                 } elseif (preg_match('@\\.(php\\d*|pl)@', $file['filename'], $matches)) {
                     self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                 }
             }
             self::verify($id);
             self::protect($app_id);
             $target_path = wa()->getDataPath("themes/{$id}", true, $app_id, false);
             waFiles::delete($target_path);
             if ($extract_path && !$tar_object->extractModify($target_path, $extract_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             } elseif (!$tar_object->extract($target_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             }
             $instance = new self($id, $app_id);
             $instance->check();
         } catch (Exception $ex) {
             if (isset($target_path) && $target_path) {
                 waFiles::delete($target_path, true);
             }
             throw $ex;
         }
     } else {
         self::throwArchiveException('UNSUPPORTED_ARCHIVE_TYPE');
     }
     return $instance;
 }
 protected function formalizeData($result)
 {
     if ($this->prefix) {
         $pattern = wa_make_pattern($this->prefix, '@');
         $pattern = "@^{$pattern}(.+)\$@";
         $order_id = null;
         if (preg_match($pattern, $this->order_id, $matches)) {
             $order_id = $matches[1];
         }
     } else {
         $order_id = $this->order_id;
     }
     $transaction_data = parent::formalizeData(null);
     $transaction_data['native_id'] = $this->txn;
     $transaction_data['amount'] = is_object($result) && property_exists(get_class($result), 'amount') && !empty($result->amount) ? str_replace(',', '.', $result->amount) : 0;
     $transaction_data['currency_id'] = 'RUB';
     $transaction_data['order_id'] = $order_id;
     if (is_object($result) && property_exists(get_class($result), 'user') && !empty($result->user)) {
         $data['phone'] = $result->user;
         $transaction_data['view_data'] = 'Phone: ' . $result->user;
     }
     if (is_object($result) && property_exists(get_class($result), 'status') && !empty($result->status)) {
         $transaction_data['view_status'] = $this->getBillCodeDescription(intval($result->status));
     }
     return $transaction_data;
 }
 protected function formalizeData($transaction_raw_data)
 {
     $transaction_data = parent::formalizeData($transaction_raw_data);
     $transaction_data['native_id'] = (string) $this->xml->transaction_id;
     $transaction_data['amount'] = (string) $this->xml->amount;
     $transaction_data['currency_id'] = (string) $this->xml->currency;
     $order_id = null;
     if (preg_match($this->pattern, (string) $this->xml->order_id, $matches)) {
         $order_id = $matches[3];
     }
     if ($this->bugfix) {
         $order_id = preg_replace('/_\\d{1,4}$/', '', $order_id);
     }
     if ($this->order_prefix) {
         $pattern = wa_make_pattern($this->order_prefix, '@');
         $pattern = "@^{$pattern}(.+)\$@";
         if (preg_match($pattern, $order_id, $matches)) {
             $order_id = $matches[1];
         } else {
             $order_id = null;
         }
     }
     $transaction_data['order_id'] = $order_id;
     $view_data = array();
     if ((string) $this->xml->transaction_id) {
         $view_data[] = $this->_w('Transaction number') . ': ' . (string) $this->xml->transaction_id;
     }
     if ((string) $this->xml->pay_way) {
         $view_data[] = $this->_w('Pay way') . ': ' . (string) $this->xml->pay_way;
     }
     switch ($status = (string) $this->xml->status) {
         case 'success':
             /*покупка совершена*/
             $transaction_data['state'] = self::STATE_CAPTURED;
             $transaction_data['type'] = self::OPERATION_AUTH_CAPTURE;
             $transaction_data['result'] = 1;
             break;
         case 'failure':
             /*покупка отклонена*/
             $transaction_data['state'] = self::STATE_DECLINED;
             $transaction_data['type'] = self::OPERATION_CANCEL;
             $transaction_data['result'] = 1;
             $view_data[] = $this->_w('Transaction declined') . ": " . (string) $this->xml->code;
             break;
         case 'wait_secure':
             /*платеж находится на проверке*/
             $view_data[] = $this->_w('Transaction requires confirmation');
             break;
         default:
             $view_data[] = sprintf($this->_w("Unknown status %s"), htmlentities($status, ENT_QUOTES, 'utf-8'));
             break;
     }
     if ((string) $this->xml->sender_phone) {
         $view_data[] = $this->_w('Phone number') . ': ' . (string) $this->xml->sender_phone;
     }
     if ($view_data) {
         $transaction_data['view_data'] = implode("\n", $view_data);
     }
     return $transaction_data;
 }
 /**
  *
  * @param $data - get from gateway
  * @return array
  */
 protected function callbackHandler($data)
 {
     if ($this->prefix) {
         $pattern = wa_make_pattern($this->prefix, '@');
         $pattern = "@^{$pattern}(.+)\$@";
         $order_id = null;
         if (preg_match($pattern, $this->order_id, $matches)) {
             $this->order_id = $matches[1];
         }
     }
     $result = null;
     if (!empty($data['result']) && $this->order_id) {
         //handle customer redirection
         $transaction_data = array('order_id' => $this->order_id);
         $type = $data['result'] == 'success' ? waAppPayment::URL_SUCCESS : waAppPayment::URL_FAIL;
         $result = array();
         $result['url'] = $this->getAdapter()->getBackUrl($type, $transaction_data);
         $result['template'] = $this->path . '/templates/result.html';
     } else {
         switch ($this->callback_protocol) {
             case self::SOAP:
                 $result = $this->soapCallbackHandler($data);
                 break;
             case self::REST:
                 $result = $this->restCallbackHandler($data);
                 break;
         }
     }
     return $result;
 }