[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Actions.class.php (source)

   1  <?php
   2  class MP_Actions
   3  {
   4  	function __construct()
   5      {
   6          switch (true)
   7          {
   8              case ( ( isset($_GET['tg']) ) && !( isset($_POST['action']) || isset($_GET['action']) ) ) :
   9                  $action = 'tracking';
  10              break;
  11              case ( isset($_POST['action']) ) :
  12                  $action = $_POST['action'];
  13              break;
  14              case ( isset($_GET['action']) ) :
  15                  $action = $_GET['action'];
  16              break;
  17              default :
  18                  MP_::mp_die(-1);
  19              break;
  20          }
  21          $action = str_replace('-', '_', $action);
  22  
  23          if ( method_exists($this, $action) ) call_user_func_array( array($this, $action), array() );
  24  
  25          do_action('mp_action_' . $action );
  26      }
  27  
  28  
  29  ////  SUBSCRIPTION FORM  ////
  30  
  31  	public static function add_user_fo() {
  32  
  33          list($message, $email, $name) = MP_Widget::insert();
  34  
  35          $xml = "<message><![CDATA[$message]]></message><id><![CDATA[" . $_POST['id'] . "]]></id><email><![CDATA[$email]]></email><name><![CDATA[$name]]></name>";
  36  
  37          ob_end_clean();
  38          header('Content-Type: text/xml');
  39          MP_::mp_die("<?xml version='1.0' standalone='yes'?><wp_ajax>$xml</wp_ajax>");
  40      }
  41  
  42  	public static function get_form()
  43      {
  44          if (!isset($_GET['iframe'])) return;
  45  
  46          $root = MP_CONTENT_DIR . 'advanced/subscription-form';
  47          $root = apply_filters('MailPress_advanced_subscription-form_root', $root);
  48          $file = "$root/iframes/" . $_GET['iframe'] . '/index.php';
  49          $dir  = dirname($file);
  50  
  51          if (is_dir($dir))
  52          {
  53              if (is_file($file)) 
  54              {
  55                  unset($_GET['action'], $_GET['iframe']);
  56  
  57                  $olddir = getcwd();
  58                  chdir($dir);
  59  
  60                  include('index.php');
  61  
  62                  chdir($olddir);
  63                  return;
  64              }
  65          }
  66  
  67          echo MailPress::shortcode();
  68      }
  69  
  70  ////  MAIL LINKS  ////
  71  
  72  	public static function tracking()
  73      {
  74          $meta = MP_Mail_meta::get_by_id($_GET['mm']);
  75          if ($meta)
  76          {
  77              do_action('mp_action_tracking', $meta); // will activate if any !
  78              switch ($_GET['tg'])
  79              {
  80                  case ('l') :
  81                      switch ($meta->meta_value)
  82                      {
  83                          case '{{subscribe}}' :
  84                              $url = MP_User::get_subscribe_url($_GET['us']);
  85                          break;
  86                          case '{{unsubscribe}}' :
  87                              $url = MP_User::get_unsubscribe_url($_GET['us']);
  88                          break;
  89                          case '{{viewhtml}}' :
  90                              $url = MP_User::get_view_url($_GET['us'], $meta->mp_mail_id);
  91                          break;
  92                          default :
  93                              $url = $meta->meta_value;
  94                          break;
  95                      }
  96                      MP_::mp_redirect($url);
  97                  break;
  98                  case ('o') :
  99                      self::download('_.gif', MP_ABSPATH . 'mp-includes/images/_.gif', 'image/gif', 'gif_' . $_GET['us'] . '_' . $_GET['mm'] . '.gif');
 100                  break;
 101              }
 102          }
 103          MP_::mp_redirect(home_url());
 104      }
 105  
 106  	public static function mail_link() 
 107      {
 108          include  (MP_ABSPATH . 'mp-includes/html/mail_link.php');
 109      }
 110  
 111  ////  DELETE  ////
 112  
 113  	public static function delete_mail() 
 114      {
 115          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 116          MP_::mp_die( MP_Mail::set_status( $id, 'delete' ) ? 1 : 0 );
 117      }
 118  
 119  	public static function delete_user() 
 120      {
 121          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 122          MP_::mp_die( MP_User::set_status( $id, 'delete' ) ? 1 : 0 );
 123      }
 124  
 125  	public static function delete_mailmeta()
 126      {
 127          if ( !current_user_can( 'MailPress_mail_custom_fields') )    MP_::mp_die(-1);
 128  
 129          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 130          check_ajax_referer( "delete-mailmeta_$id" );
 131  
 132          MP_::mp_die( MP_Mail_meta::delete_by_id( $id ) ? 1 : 0 );
 133      }
 134  
 135  	public static function delete_usermeta()
 136      {
 137          if ( !current_user_can( 'MailPress_user_custom_fields') )    MP_::mp_die(-1);
 138  
 139          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 140          check_ajax_referer( "delete-usermeta_$id" );
 141  
 142          MP_::mp_die( MP_User_meta::delete_by_id( $id ) ? 1 : 0 );
 143      }
 144  
 145  	public static function delete_attachement()
 146      {
 147          if (!isset($_POST['meta_id'])) return;
 148          if (!is_numeric($_POST['meta_id'])) return;
 149  
 150          $meta_id = (int) $_POST['meta_id'];
 151          MP_Mail_meta::delete_by_id( $meta_id );
 152          MP_::mp_die(1);
 153      }
 154  
 155  //// DIM LIST ////
 156  
 157  	public static function dim_mail() 
 158      {
 159          require_once (MP_ABSPATH . 'mp-admin/mails.php');
 160  
 161          $url_parms     = MP_AdminPage::get_url_parms();
 162  
 163          $id         = isset($_POST['id']) ? (int) $_POST['id'] : 0;
 164             $status     = MP_Mail::get_status($id);
 165  
 166          $dims = array( 'sent' => 'archived', 'archived' => 'sent' );
 167  
 168          if (!isset($dims[$status])) MP_::mp_die();
 169          if (!MP_Mail::set_status( $id, $dims[$status])) MP_::mp_die(-1);
 170          
 171          ob_start();
 172              MP_AdminPage::get_row( $id, $url_parms );
 173              $html = ob_get_contents();
 174          ob_end_clean();
 175  
 176          $xml = "<rc><![CDATA[0]]></rc><id><![CDATA[$id]]></id><item><![CDATA[$html]]></item><old_status><![CDATA[$status]]></old_status><new_status><![CDATA[" . $dims[$status] . "]]></new_status>"; 
 177  
 178          ob_end_clean();
 179          header('Content-Type: text/xml');
 180          MP_::mp_die("<?xml version='1.0' standalone='yes'?><mp_action>$xml</mp_action>");
 181      }
 182  
 183  	public static function dim_user() 
 184      {
 185          require_once (MP_ABSPATH . 'mp-admin/users.php');
 186  
 187          $url_parms     = MP_AdminPage::get_url_parms();
 188  
 189             $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 190             $status = MP_User::get_status($id);
 191  
 192          $dims = array( 'unsubscribed' => 'waiting', 'waiting' => 'active', 'active' => 'waiting', 'bounced' => 'waiting' );
 193  
 194          if (!isset($dims[$status])) MP_::mp_die();
 195          if (!MP_User::set_status( $id, $dims[$status])) MP_::mp_die(-1);
 196          
 197          ob_start();
 198              MP_AdminPage::get_row( $id, $url_parms );
 199              $html = ob_get_contents();
 200          ob_end_clean();
 201  
 202          $xml = "<rc><![CDATA[0]]></rc><id><![CDATA[$id]]></id><item><![CDATA[$html]]></item><old_status><![CDATA[$status]]></old_status><new_status><![CDATA[" . $dims[$status] . "]]></new_status>"; 
 203  
 204          ob_end_clean();
 205          header('Content-Type: text/xml');
 206          MP_::mp_die("<?xml version='1.0' standalone='yes'?><mp_action>$xml</mp_action>");
 207      }
 208  
 209  //// ADD LIST ////
 210  
 211  	public static function add_mail() 
 212      {
 213          require_once (MP_ABSPATH . 'mp-admin/mails.php');
 214  
 215          $url_parms = MP_AdminPage::get_url_parms();
 216          $url_parms['paged'] = isset($url_parms['paged']) ? $url_parms['paged'] : 1;
 217          $_per_page = MP_AdminPage::get_per_page();
 218          $start = ( $url_parms['paged'] - 1 ) * $_per_page;
 219  
 220          list($mails, $total) = MP_AdminPage::get_list(array('start' => $start, '_per_page' => 1, 'url_parms' => $url_parms));
 221  
 222          if ( !$mails ) MP_::mp_die(1);
 223  
 224          $x = new WP_Ajax_Response();
 225          foreach ( (array) $mails as $mail ) 
 226          {
 227              MP_Mail::get( $mail );
 228              ob_start();
 229                  MP_AdminPage::get_row( $mail->id, $url_parms );
 230                  $html = ob_get_contents();
 231              ob_end_clean();
 232              $x->add( array(
 233                  'what'     => 'mail', 
 234                  'id'         => $mail->id, 
 235                  'data'     => $html
 236              ) );
 237          }
 238          $x->send();
 239      }
 240  
 241  	public static function add_user() 
 242      {
 243          require_once (MP_ABSPATH . 'mp-admin/users.php');
 244  
 245          $url_parms = MP_AdminPage::get_url_parms();
 246          $url_parms['paged'] = isset($url_parms['paged']) ? $url_parms['paged'] : 1;
 247          $_per_page = MP_AdminPage::get_per_page();
 248          $start = ( $url_parms['paged'] - 1 ) * $_per_page;
 249  
 250          list($users, $total) = MP_AdminPage::get_list(array('start' => $start, '_per_page' => 1, 'url_parms' => $url_parms));
 251  
 252          if ( !$users ) MP_::mp_die(1);
 253  
 254          $x = new WP_Ajax_Response();
 255          foreach ( (array) $users as $user ) {
 256              MP_User::get( $user );
 257              ob_start();
 258                  MP_AdminPage::get_row( $user->id, $url_parms, false );
 259                  $html = ob_get_contents();
 260              ob_end_clean();
 261              $x->add( array(
 262                  'what'     => 'user', 
 263                  'id'         => $user->id, 
 264                  'data'     => $html
 265              ) );
 266          }
 267          $x->send();
 268      }
 269  
 270  
 271  //// CUSTOM FIELDS ////
 272  
 273  	public static function add_mailmeta()
 274      {
 275          if ( !current_user_can( 'MailPress_mail_custom_fields') )    MP_::mp_die(-1);
 276  
 277          check_ajax_referer( 'add-mailmeta' );
 278  
 279          $c = 0;
 280          $object_id = (int) $_POST['mail_id'];
 281          if ($object_id === 0) MP_::mp_die();
 282  
 283          if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) 
 284          {
 285              if (isset($_POST['metakeyselect']) && ('#NONE#' == $_POST['metakeyselect']) && empty($_POST['metakeyinput']) )    MP_::mp_die(1);
 286              if ( !$meta_id = MP_Mail_meta::add_meta( $object_id ) )     MP_::mp_die();
 287  
 288              $response = array('position'     => 1);
 289          }
 290          else
 291          {
 292              $meta_id = (int) array_pop(array_keys($_POST['mailmeta']));
 293              $key     = $_POST['mailmeta'][$meta_id]['key'];
 294              $value   = $_POST['mailmeta'][$meta_id]['value'];
 295  
 296              if ( !$meta = MP_Mail_meta::get_by_id( $meta_id ) )        MP_::mp_die();
 297              if ( !MP_Mail_meta::update_by_id($meta_id , $key, $value) )    MP_::mp_die(1);
 298  
 299              $response = array('old_id'     => $meta_id, 'position'     => 0);
 300          }
 301  
 302          $meta = MP_Mail_meta::get_by_id( $meta_id );
 303          $object_id = (int) $meta->mp_mail_id;
 304          $meta = get_object_vars( $meta );
 305          require_once (MP_ABSPATH . 'mp-admin/write.php');
 306  
 307          $response = array_merge($response, array('what' => 'mailmeta', 'id' => $meta_id, 'data' => MP_AdminPage::meta_box_customfield_row( $meta, $c ), 'supplemental' => array('mail_id' => $object_id) ) );
 308  
 309          $x = new WP_Ajax_Response( $response );
 310  
 311          $x->send();
 312      }
 313  
 314  	public static function add_usermeta()
 315      {
 316          if ( !current_user_can( 'MailPress_user_custom_fields') )    MP_::mp_die(-1);
 317  
 318          check_ajax_referer( 'add-usermeta' );
 319  
 320          $c = 0;
 321          $object_id = (int) $_POST['mp_user_id'];
 322          if ($object_id === 0) MP_::mp_die();
 323  
 324          if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) 
 325          {
 326              if (isset($_POST['metakeyselect']) && ('#NONE#' == $_POST['metakeyselect']) && empty($_POST['metakeyinput']) )    MP_::mp_die(1);
 327              if ( !$meta_id = MP_User_meta::add_meta( $object_id ) )     MP_::mp_die();
 328  
 329              $response = array('position'     => 1);
 330          }
 331          else
 332          {
 333              $meta_id = (int) array_pop(array_keys($_POST['usermeta']));
 334              $key     = $_POST['usermeta'][$meta_id]['key'];
 335              $value   = $_POST['usermeta'][$meta_id]['value'];
 336  
 337              if ( !$meta = MP_User_meta::get_by_id( $meta_id ) )        MP_::mp_die();
 338              if ( !MP_User_meta::update_by_id($meta_id , $key, $value) )    MP_::mp_die(1);
 339  
 340              $response = array('old_id'     => $meta_id, 'position'     => 0);
 341          }
 342  
 343          $meta = MP_User_meta::get_by_id( $meta_id );
 344          $object_id = (int) $meta->mp_user_id;
 345          $meta = get_object_vars( $meta );
 346          require_once (MP_ABSPATH . 'mp-admin/user.php');
 347  
 348          $response = array_merge($response, array('what' => 'usermeta', 'id' => $meta_id, 'data' => MP_AdminPage::meta_box_customfield_row( $meta, $c ), 'supplemental' => array('mp_user_id' => $object_id) ) );
 349  
 350          $x = new WP_Ajax_Response( $response );
 351  
 352          $x->send();
 353      }
 354  
 355  ////  VIEW MAIL/THEME in thickbox  ////
 356  
 357  	public static function get_previewlink()
 358      {
 359          $args            = array();
 360          $args['action']     = 'iview';
 361          $args['id']        = (isset($_POST['id'])) ? intval($_POST['id']) : 0;
 362          $args['main_id']    = (isset($_POST['main_id'])) ? intval($_POST['main_id']) : 0;
 363          $args['preview_iframe'] = 1;
 364          $args['TB_iframe']= 'true';
 365  
 366          $url = esc_url(add_query_arg( $args, MP_Action_url ));
 367          MP_::mp_die($url);
 368      }
 369  
 370  	public static function iview()
 371      {
 372          $mp_general = get_option(MailPress::option_name_general);
 373  
 374          $id         = $_GET['id'];
 375          $main_id    = (isset($_GET['main_id'])) ? $_GET['main_id'] : $id;
 376  
 377          $mail     = MP_Mail::get($id);
 378  
 379          $theme     = (isset($_GET['theme']) && !empty($_GET['theme'])) ? $_GET['theme'] : (!empty($mail->theme) ? $mail->theme : false);
 380          $mp_user_id    = (isset($_GET['mp_user_id'])  && !empty($_GET['mp_user_id']))  ? $_GET['mp_user_id']  : false;
 381  
 382      // from
 383          $from     = (!empty($mail->fromemail)) ? MP_Mail::display_toemail($mail->fromemail, $mail->fromname) : MP_Mail::display_toemail($mp_general['fromemail'], $mp_general['fromname']);
 384      // to
 385          $to         = MP_Mail::display_toemail($mail->toemail, $mail->toname, '', $mp_user_id);
 386      // subject
 387          $x = new MP_Mail();
 388          $subject     = (in_array($mail->status, array('sent', 'archived'))) ? $mail->subject : $x->do_eval($mail->subject);
 389          $subject     = $x->viewsubject($subject, $id, $main_id, $mp_user_id);
 390      // template
 391          $template   = (in_array($mail->status, array('sent', 'archived'))) ? false : apply_filters('MailPress_draft_template', false, $main_id);
 392  
 393      // content
 394          $args            = array();
 395          $args['action']     = 'viewadmin';
 396          foreach(array('id', 'main_id', 'theme', 'template', 'mp_user_id') as $x) if ($$x) $args[$x] = $$x;
 397  
 398          foreach(array('html', 'plaintext') as $type)
 399          {
 400              $args['type'] = $type;
 401              if (!empty($mail->{$type})) $$type = "<iframe id='i{$type}' style='width:100%;border:0;height:550px' src='" . esc_url(add_query_arg( $args, MP_Action_url )) . "'></iframe>";
 402          }
 403  
 404      // attachements
 405          $attachements = '';
 406          $metas = MP_Mail_meta::has( $args['main_id'], '_MailPress_attached_file');
 407          if ($metas) foreach($metas as $meta) $attachements .= "<tr><td>&#160;" . MP_Mail::get_attachement_link($meta, $mail->status) . "</td></tr>";
 408          $view = true;
 409          include (MP_ABSPATH . 'mp-includes/html/mail.php');
 410      }
 411  
 412  	public static function viewadmin() 
 413      {
 414          $_GET['type'] = (isset($_GET['type'])) ? $_GET['type'] : 'html';
 415          $_GET['template'] = apply_filters('MailPress_draft_template', isset($_GET['template']) ? $_GET['template'] : false, $_GET['main_id']);
 416  
 417          $x = new MP_Mail();
 418          $x->view($_GET);
 419      }
 420  
 421  	public static function view() 
 422      {
 423          $id         = $_GET['id'];
 424          $key        = $_GET['key'];
 425          $email     = MP_User::get_email(MP_User::get_id($key));
 426          //if (empty($email)) wp_die(__('Wrong arguments in url', MP_TXTDOM));
 427          $mail     = MP_Mail::get($id);
 428  
 429          if (!is_email($mail->toemail))
 430          {
 431              $m = MP_Mail_meta::get($id, '_MailPress_replacements');
 432              if (!is_array($m)) $m = array();
 433  
 434              $recipients = unserialize($mail->toemail);
 435              $replacements = (isset($recipients[$email])) ? array_merge($m, $recipients[$email]) : array_merge($m, array('{{_confkey}}' => 0));
 436  
 437              foreach(array('html', 'plaintext') as $type) if (!empty($mail->{$type})) {foreach($replacements as $k => $v) $mail->{$type} = str_replace($k, $v, $mail->{$type}, $ch); break;};
 438          }
 439  
 440          if (!empty($mail->html))
 441          {
 442              $x = new MP_Mail();
 443              echo $x->process_img($mail->html, $mail->themedir, 'draft');
 444          }
 445          elseif (!empty($mail->plaintext))
 446              echo '<pre>' . htmlspecialchars($mail->plaintext, ENT_NOQUOTES) . '</pre>';
 447      }
 448  
 449  ////  THEMES  ////
 450  
 451  	public static function theme_preview() 
 452      {
 453          $args            = array( 'action'    => 'previewtheme', 'template' => $_GET['template'], 'stylesheet'=> $_GET['stylesheet'] );
 454  
 455          foreach(array('html', 'plaintext') as $type)
 456          {
 457              $args['type']     = $type;
 458              $$type        = "<iframe id='i{$type}' style='width:100%;border:0;height:550px' src='" . esc_url(add_query_arg( $args, MP_Action_url )) . "'></iframe>";
 459          }
 460  
 461          unset($view);
 462          include  (MP_ABSPATH . 'mp-includes/html/mail.php');
 463      }
 464  
 465  	public static function previewtheme() 
 466      {
 467          $url             = home_url();
 468  
 469          $mail            = new stdClass();
 470          $mail->Theme     = $_GET['stylesheet'];
 471          $mail->Template     = 'confirmed';
 472  
 473          $message  = __('Congratulations !', MP_TXTDOM);
 474          $message .= "\n\n";
 475          $message .= sprintf(__('We confirm your subscription to %1$s emails', MP_TXTDOM), get_bloginfo('name') );
 476          $message .= "\n\n";
 477  
 478          $mail->plaintext     = $message;
 479  
 480          $message  = __('Congratulations !', MP_TXTDOM);
 481          $message .= '<br /><br />';
 482          $message .= sprintf(__('We confirm your subscription to %1$s emails', MP_TXTDOM), "<a href='$url'>" . get_bloginfo('name') . "</a>" );
 483          $message .= '<br /><br />';
 484  
 485          $mail->html     = $message;
 486  
 487          $mail->unsubscribe= __('"Subscription management link"', MP_TXTDOM);
 488          $mail->viewhtml     = __('"Trouble reading link"', MP_TXTDOM);
 489  
 490          $x = new MP_Mail();
 491          $x->args = new stdClass();
 492          $x->args = $mail;
 493  
 494          $type  = $_GET['type'];
 495          $$type = $x->build_mail_content($type);
 496          $$type = ('html' == $type) ? $x->process_img($$type, $x->mail->themedir, 'draft') : $$type;
 497          include MP_ABSPATH . "mp-includes/html/{$type}.php";
 498      }
 499  
 500  
 501  ////  WRITE  ////
 502  
 503  	public static function html2txt() 
 504      {
 505          if (!isset($_POST['html'])) return '';
 506          $content = trim(stripslashes($_POST['html']));
 507          if (empty($content)) return '';
 508  
 509          $txt = new MP_Html2txt();
 510          echo trim($txt->get_text( apply_filters('the_content', $content), 0 ), " \r\n");
 511          die();
 512      }
 513  
 514  	public static function autosave()
 515      {
 516          global $current_user;
 517  
 518          $data = '';
 519          $supplemental = array();
 520          $do_lock     = true;
 521  
 522          $working_id = $main_id     = (int) $_POST['id'];
 523          $do_autosave= (bool) $_POST['autosave'];
 524  
 525          if ( -1 == $_POST['revision'])
 526          {
 527              if ( $do_autosave ) 
 528              {
 529                  if (!$working_id) $working_id = $main_id = MP_Mail::get_id(__CLASS__ . ' 1 ' . __METHOD__);
 530  
 531                  MP_Mail_draft::update($working_id);
 532                  $data = sprintf( __('Draft saved at %s.', MP_TXTDOM), date( __('g:i:s a'), current_time( 'timestamp' ) ) );
 533                  $supplemental['tipe'] = 'mail';
 534              }
 535          }
 536          else
 537          {
 538              if ( $last = MP_Mail_lock::check( $main_id ) ) 
 539              {
 540                  $do_autosave     = $do_lock = false;
 541                  $last_user         = get_userdata( $last );
 542                  $last_user_name     = ($last_user) ? $last_user->display_name : __( 'Someone' );    
 543                  $data         = new WP_Error( 'locked', sprintf( __( 'Autosave disabled: %s is currently editing this mail.' ) , esc_html( $last_user_name )    ) );
 544                  $supplemental['disable_autosave'] = 'disable';
 545              }
 546  
 547              if ( $do_autosave ) 
 548              {
 549                  $working_id = (int) $_POST['revision'];
 550                  if (!$working_id)
 551                  {
 552                      $working_id = MP_Mail::get_id(__CLASS__ . ' 2 ' . __METHOD__);
 553  
 554                      $mailmetas = MP_Mail_meta::get( $main_id, '_MailPress_mail_revisions');
 555                      $mailmetas[$current_user->ID] = $working_id;
 556  
 557                      if (!MP_Mail_meta::add(    $main_id, '_MailPress_mail_revisions', $mailmetas, true))
 558                          MP_Mail_meta::update($main_id, '_MailPress_mail_revisions', $mailmetas);
 559                  }
 560  
 561                  MP_Mail_draft::update($working_id, '');
 562                  $data = sprintf( __('Revision saved at %s.', MP_TXTDOM), date( __('g:i:s a'), current_time( 'timestamp', true ) ) );
 563                  $supplemental['tipe'] = 'revision';
 564              }
 565              else
 566              {
 567                  if ($_POST['revision']) $working_id = (int) $_POST['revision'];
 568                  $supplemental['tipe'] = 'revision';
 569              }
 570          }
 571  
 572          if ( $do_lock && $working_id ) MP_Mail_lock::set( $main_id );
 573  
 574          $x = new WP_Ajax_Response( array (    'what'     => 'autosave', 
 575                                  'id'         => $working_id, 
 576                                  'old_id'     => $main_id, 
 577                                  'type'     => false, 
 578                                  'data'     => $working_id ? $data : '', 
 579                                  'supplemental' => $supplemental
 580          ) );
 581  
 582          $x->send();
 583      }
 584  
 585  ////  ATTACHEMENTS  UPLOAD  ////
 586  
 587  	public static function upload_iframe_html()
 588      {
 589          $id         = $_GET['id'];
 590          $draft_id     = $_GET['draft_id'];
 591          $bytes     = apply_filters('import_upload_size_limit', wp_max_upload_size() );
 592  
 593          wp_register_script( 'upload_iframe', '/' . MP_PATH . 'mp-includes/js/fileupload/upload_iframe.js', array('jquery'), false, 1);
 594          wp_localize_script( 'upload_iframe', 'uploadhtmlL10n', array( 
 595              'id' => $id
 596          ) );
 597          wp_enqueue_script('upload_iframe');
 598  
 599          include  MP_ABSPATH . 'mp-includes/html/upload_iframe.php';
 600      }
 601  
 602  	public static function swfu_mail_attachement() 
 603      {
 604          // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
 605          if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
 606              $_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
 607          elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
 608              $_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
 609  
 610          $xml = self::mail_attachement();
 611  
 612          ob_end_clean();
 613          header('Content-Type: text/xml');
 614          echo $xml;
 615          die();
 616      }
 617  
 618  	public static function html_mail_attachement() 
 619      {
 620          $draft_id     = $_REQUEST['draft_id'];
 621          $id        = $_REQUEST['id'];
 622          $file        = $_REQUEST['file'];
 623  
 624          $xml = self::mail_attachement();
 625  
 626          $xml = str_replace('>', '&gt;', $xml);
 627          $xml = str_replace('<', '&lt;', $xml);
 628  
 629          wp_register_script( 'upload_iframe_xml', '/' . MP_PATH . 'mp-includes/js/fileupload/upload_iframe_xml.js', array('jquery'), false, 1);
 630          wp_localize_script( 'upload_iframe_xml', 'uploadxmlL10n', array(
 631              'id'        => $id,
 632              'draft_id'     => $draft_id,
 633              'file'     => $file
 634          ) );
 635          wp_enqueue_script('upload_iframe_xml');
 636  
 637          ob_end_clean();
 638          ob_start();
 639              include  MP_ABSPATH . 'mp-includes/html/upload_iframe_xml.php';
 640              $html = ob_get_contents();
 641          ob_end_clean();
 642  
 643          MP_::mp_die($html);
 644      }
 645  
 646  	public static function mail_attachement()
 647      {
 648          $data = self::handle_upload('async-upload', $_REQUEST['draft_id']);
 649  
 650          if (is_wp_error($data)) 
 651          {
 652              $xml  = "<error><![CDATA[" . $data->get_error_message() . "]]></error>";
 653          }
 654          else
 655          {
 656              $xml  = "<id><![CDATA[" . $data['id'] . "]]></id>";
 657              $xml .= "<url><![CDATA[" . $data['url'] . "]]></url>";
 658              $xml .= "<file><![CDATA[" . $data['file'] . "]]></file>";
 659          }
 660  
 661          return "<?xml version='1.0' standalone='yes'?><mp_fileupload>$xml</mp_fileupload>";
 662      }
 663  
 664  	public static function handle_upload($file_id, $draft_id) 
 665      {
 666          $overrides = array('test_form'=>false, 'unique_filename_callback' => 'mp_unique_filename_callback');
 667          $time = current_time('mysql');
 668  
 669          $uploaded_file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
 670  
 671          if ( isset($uploaded_file['error']) )
 672              return new WP_Error( 'upload_error', $uploaded_file['error'] );
 673  
 674  // Check file path is ok
 675          $uploads = wp_upload_dir();
 676          if ( $uploads && (false === $uploads['error']) )                             // Get upload directory
 677          {     
 678              if ( 0 === strpos($uploaded_file['file'], $uploads['basedir']) )                 // Check that the upload base exists in the file path
 679              {
 680                  $file = str_replace($uploads['basedir'], '', $uploaded_file['file']);         // Remove upload dir from the file path
 681                  $file = ltrim($file, '/');
 682              }
 683          }
 684  
 685  // Construct the attachment array
 686          $object = array(
 687                      'name'     => $_FILES['async-upload']['name'], 
 688                      'mime_type'    => $uploaded_file['type'], 
 689                      'file'    => $file, 
 690                      'file_fullpath'    => str_replace("\\", "/", $uploaded_file['file']), 
 691                      'guid'     => $uploaded_file['url']
 692                  );
 693  // Save the data
 694          $id = MP_Mail_meta::add( $draft_id, '_MailPress_attached_file', $object );
 695  
 696          $href = esc_url(add_query_arg( array('action' => 'attach_download', 'id' => $id), MP_Action_url ));
 697          return array('id' => $id, 'url' => $href, 'file' => $object['file_fullpath']);
 698      }
 699  
 700  
 701  	public static function attach_download()
 702      {
 703          $meta_id     = (int) $_GET['id'];
 704  
 705          $meta = MP_Mail_meta::get_by_id($meta_id);
 706  
 707          if (!$meta) MP_::mp_die(__('Cannot Open Attachment 1!', MP_TXTDOM));
 708          if (!is_file($meta->meta_value['file_fullpath']))    MP_::mp_die(__('Cannot Open Attachment 2! ' . $meta->meta_value['file_fullpath'], MP_TXTDOM));
 709  
 710          self::download($meta->meta_value['name'], $meta->meta_value['file_fullpath'], $meta->meta_value['mime_type']);
 711      }
 712  
 713  
 714  ////  MISC  ////
 715  
 716  	public static function download($file, $file_fullpath, $mime_type, $name = false)
 717      {
 718          if (!$name) $name = $file;
 719          if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) $file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
 720  
 721          if(!$fdl = @fopen($file_fullpath, 'r'))     MP_::mp_die(__('Cannot Open File !', MP_TXTDOM));
 722  
 723          header("Cache-Control: ");# leave blank to avoid IE errors
 724          header("Pragma: ");# leave blank to avoid IE errors
 725          header("Content-type: " . $mime_type);
 726          header("Content-Disposition: attachment; filename=\"".$file."\"");
 727          header("Content-length:".(string)(filesize($file_fullpath)));
 728          sleep(1);
 729          fpassthru($fdl);
 730          MP_::mp_die();
 731      }
 732  
 733  	public static function map_settings()
 734      {
 735          if ('mp_user' == $_POST['type'])
 736          {
 737              if (!MP_User_meta::add(     $_POST['id'], '_MailPress_' . $_POST['prefix'], $_POST['settings'], true ))
 738                  MP_User_meta::update( $_POST['id'], '_MailPress_' . $_POST['prefix'], $_POST['settings'] );
 739          }
 740          else
 741          {
 742              if (!MP_Mail_meta::add(     $_POST['id'], '_MailPress_' . $_POST['prefix'], $_POST['settings'], true ))
 743                  MP_Mail_meta::update( $_POST['id'], '_MailPress_' . $_POST['prefix'], $_POST['settings'] );
 744          }
 745          MP_::mp_die();
 746  
 747          update_user_meta( MP_WP_User::get_id(), '_MailPress_' . $_POST['prefix'], $_POST['settings'] );
 748          MP_::mp_die();
 749      }
 750  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1