controller.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Common logic for all shortcodes plugin implements |
| 4 | * |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | */ |
| 7 | abstract class PMXE_Controller { |
| 8 | /** |
| 9 | * Input class instance to retrieve parameters submitted during page request |
| 10 | * @var PMXE_Input |
| 11 | */ |
| 12 | protected $input; |
| 13 | /** |
| 14 | * Error messages |
| 15 | * @var WP_Error |
| 16 | */ |
| 17 | protected $errors; |
| 18 | /** |
| 19 | * Associative array of data which will be automatically available as variables when template is rendered |
| 20 | * @var array |
| 21 | */ |
| 22 | public $data = array(); |
| 23 | /** |
| 24 | * Constructor |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $this->input = new PMXE_Input(); |
| 28 | $this->input->addFilter('trim'); |
| 29 | |
| 30 | $this->errors = new WP_Error(); |
| 31 | |
| 32 | $this->init(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Method to put controller initialization logic to |
| 37 | */ |
| 38 | protected function init() {} |
| 39 | |
| 40 | /** |
| 41 | * Checks wether protocol is HTTPS and redirects user to secure connection if not |
| 42 | */ |
| 43 | protected function force_ssl() { |
| 44 | if (force_ssl_admin() && ! is_ssl()) { |
| 45 | if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { |
| 46 | wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); die(); |
| 47 | } else { |
| 48 | wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); die(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Method returning resolved template content |
| 55 | * |
| 56 | * @param string [optional] $viewPath Template path to render |
| 57 | * @throws Exception |
| 58 | */ |
| 59 | protected function render($viewPath = null) { |
| 60 | |
| 61 | if ( ! get_current_user_id() or ! current_user_can( PMXE_Plugin::$capabilities )) { |
| 62 | // This nonce is not valid. |
| 63 | die( 'Security check' ); |
| 64 | |
| 65 | } else { |
| 66 | |
| 67 | // assume template file name depending on calling function |
| 68 | if (is_null($viewPath)) { |
| 69 | $trace = debug_backtrace(); |
| 70 | $viewPath = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXE_Plugin::PREFIX, '%') . '%', '', strtolower($trace[1]['class']))) . '/' . $trace[1]['function']; |
| 71 | } |
| 72 | // append file extension if not specified |
| 73 | if ( ! preg_match('%\.php$%', $viewPath)) { |
| 74 | $viewPath .= '.php'; |
| 75 | } |
| 76 | $filePath = PMXE_Plugin::ROOT_DIR . '/views/' . $viewPath; |
| 77 | if (is_file($filePath)) { |
| 78 | extract($this->data); |
| 79 | include $filePath; |
| 80 | } else { |
| 81 | throw new Exception("Requested template file $filePath is not found."); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Display list of errors |
| 88 | * |
| 89 | * @param string|array|WP_Error[optional] $msgs |
| 90 | */ |
| 91 | protected function error($msgs = NULL) { |
| 92 | if (is_null($msgs)) { |
| 93 | $msgs = $this->errors; |
| 94 | } |
| 95 | if (is_wp_error($msgs)) |
| 96 | { |
| 97 | unset($msgs->errors['count-validation']); |
| 98 | |
| 99 | $msgs = $msgs->get_error_messages(); |
| 100 | } |
| 101 | if ( ! is_array($msgs)) { |
| 102 | $msgs = array($msgs); |
| 103 | } |
| 104 | $this->data['errors'] = $msgs; |
| 105 | |
| 106 | $viewPathRel = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXE_Plugin::PREFIX, '%') . '%', '', strtolower(get_class($this)))) . '/error.php'; |
| 107 | if (is_file(PMXE_Plugin::ROOT_DIR . '/views/' . $viewPathRel)) { // if calling controller class has specific error view |
| 108 | $this->render($viewPathRel); |
| 109 | } else { // render default error view |
| 110 | $this->render('controller/error.php'); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function download(){ |
| 115 | |
| 116 | |
| 117 | $nonce = (!empty($_REQUEST['_wpnonce'])) ? $_REQUEST['_wpnonce'] : ''; |
| 118 | if ( ! wp_verify_nonce( $nonce, '_wpnonce-download_feed' ) ) { |
| 119 | die( __('Security check', 'wp_all_export_plugin') ); |
| 120 | } else { |
| 121 | |
| 122 | $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); |
| 123 | |
| 124 | $id = $this->input->get('id'); |
| 125 | |
| 126 | $export = new PMXE_Export_Record(); |
| 127 | |
| 128 | $filepath = ''; |
| 129 | |
| 130 | if ( ! $export->getById($id)->isEmpty()) |
| 131 | { |
| 132 | if($export->options['export_to'] != XmlExportEngine::EXPORT_TYPE_GOOLE_MERCHANTS && isset($_GET['google_feed'])) { |
| 133 | die('Unauthorized'); |
| 134 | } |
| 135 | if ( ! $is_secure_import) |
| 136 | { |
| 137 | $filepath = get_attached_file($export->attch_id); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | $filepath = wp_all_export_get_absolute_path($export->options['filepath']); |
| 142 | } |
| 143 | if ( @file_exists($filepath) ) |
| 144 | { |
| 145 | switch ($export['options']['export_to']) |
| 146 | { |
| 147 | case XmlExportEngine::EXPORT_TYPE_XML: |
| 148 | |
| 149 | if($export['options']['xml_template_type'] == XmlExportEngine::EXPORT_TYPE_GOOLE_MERCHANTS) { |
| 150 | PMXE_download::txt($filepath); |
| 151 | } else { |
| 152 | PMXE_download::xml($filepath); |
| 153 | } |
| 154 | |
| 155 | break; |
| 156 | case XmlExportEngine::EXPORT_TYPE_CSV: |
| 157 | if (empty($export->options['export_to_sheet']) or $export->options['export_to_sheet'] == 'csv') |
| 158 | { |
| 159 | PMXE_download::csv($filepath); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | switch ($export->options['export_to_sheet']){ |
| 164 | case 'xls': |
| 165 | PMXE_download::xls($filepath); |
| 166 | break; |
| 167 | case 'xlsx': |
| 168 | PMXE_download::xlsx($filepath); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |