<?php
/*
Copyright 2012 Neil Harvey - wordpressLAB
*/

Class WP_SA_SETTING {
    function default_setting() {
        $wp_sa_setting['wp_sa_default'] = 1;
        $wp_sa_setting['salesautomator_currency_code'] = 'USD';
        $wp_sa_setting['salesautomator_user_msg'] = '<p>You will now be redirected to PayPal<br/>"."for payment processing.</p>"."<p>You will return to this site once you<br/>"."have completed the payment process.</p>';
        $wp_sa_setting['salesautomator_currency_code'] = 'Login/Register for more.';
        $wp_sa_setting['salesautomator_access_denied_msg'] = 'We are sorry. You do not have access to this area, please <a href=wp-login.php>login</a>.';
        $wp_sa_setting['sa_thankyou_msg'] = '<p><br><b>Thanks for purchasing!</br> Please check your paypal email for the download link(s)!';
        $wp_sa_setting['rt_closed_user_group'] = array('administrator' => 'Administrator');
        update_option('wp_sa_setting', $wp_sa_setting);
    }

    function get_setting() {
        return get_option('wp_sa_setting');        
    }
    
    function update_setting() {        
        if ( !current_user_can('manage_options') )  die(__('Cheatin&#8217; uh?'));
        $obj = new WP_SA_SETTING;

        $wp_sa_setting = $obj->get_setting();
        if( !isset ($wp_sa_setting['wp_sa_default'] ) ) $obj->default_setting();
        $wp_sa_setting = $obj->get_setting();

        $wp_sa_setting['wp_sa_test'] = $_POST['wp_sa_test'];
        $wp_sa_setting['wp_sa_payment_email'] = trim($_POST['wp_sa_payment_email']) ;
        $wp_sa_setting['wp_sa_currency_code'] = trim($_POST['wp_sa_currency_code']) ;
        $wp_sa_setting['wp_sa_subscription_button_image'] = trim($_POST['wp_sa_subscription_button_image']) ;
        $wp_sa_setting['wp_sa_more_login'] = trim($_POST['wp_sa_more_login']) ;
        $wp_sa_setting['wp_sa_access_denied_msg'] = trim($_POST['wp_sa_access_denied_msg']) ;
        $wp_sa_setting['wp_sa_closed_user_group'] = $_POST['wp_sa_closed_user_group'];
        $wp_sa_setting['wp_sa_login_redirection_url'] = empty($_POST['wp_sa_login_redirection_url']) ? get_bloginfo('url') : $_POST['wp_sa_login_redirection_url'];
        $wp_sa_setting['wp_sa_sub_payment_url'] = empty($_POST['wp_sa_sub_payment_url']) ? get_bloginfo('url') : $_POST['wp_sa_sub_payment_url'];
        $wp_sa_setting['wp_sa_reset_dnld_url'] = empty($_POST['wp_sa_reset_dnld_url']) ? get_bloginfo('url') : $_POST['wp_sa_reset_dnld_url'];
        $wp_sa_setting['wp_sa_thank_you_page'] = empty($_POST['wp_sa_thank_you_page']) ? get_bloginfo('url') : $_POST['wp_sa_thank_you_page'];
        $wp_sa_setting['wp_sa_upload_dir_path'] = trim($_POST['wp_sa_upload_dir_path']) ;
        $wp_sa_setting['wp_sa_display_recurring'] = $_POST['wp_sa_display_recurring'];
        $wp_sa_setting['wp_sa_keep_data'] = $_POST['wp_sa_keep_data'];
        $wp_sa_setting['wp_sa_product_button_image'] = trim($_POST['wp_sa_product_button_image']) ;
        
        if($_POST['wp_sa_upload_dir_path']!=null) {
            if($this->create_file_and_directory($_POST['wp_sa_upload_dir_path'],'.htaccess','Order Deny,Allow \nDeny from all')) {
                $wp_sa_setting['wp_sa_upload_dir_path'] = trim($_POST['wp_sa_upload_dir_path']) ;
            }
            else {
                $errormsg1= "Please check the path Make Directory Using : sudo mkdir". $_POST['wp_sa_upload_dir_path']." and Change Permissions Using Command : sudo chmod a+rw". $_POST['wp_sa_upload_dir_path'] ;
            }
        }
        //save mail setting
        $wp_sa_setting['wp_sa_max_count_limit'] = $_POST['wp_sa_max_count_limit'];
        $hours = 0;
        if($_POST['wp_sa_days'] != '0') { $hours = $_POST['wp_sa_days'] * 24; }
        $hours = $hours +  $_POST['wp_sa_hour'];
        
       // if ($hours < 10) { $hours = 10; }
        $wp_sa_setting['rt_mail_option_hours'] = $hours;

        update_option('wp_sa_setting', $wp_sa_setting);
    }

    function setting_page() {
        global $wp_roles;
        $obj = new WP_SA_SETTING;
        if ( isset($_POST['wp_sa_actionn']) && $_POST['wp_sa_actionn'] == 'wp_sa_submit' )
            $obj->update_setting();
        
        $wp_sa_setting = $obj->get_setting();        
        ?>
        <div class="wrap">
            <h2><?php _e('Configuration'); ?></h2>
            <?php
            if( isset( $errormsg1 ) ) echo '<div class="error">'.$errormsg1.'</div>';
            if( isset ($errormsg2 ) ) echo '<div class="error">'.$errormsg2.'</div>';
            ?>
            <h3><?php _e('PayPal Configuration'); ?></h3>
            <form action="" method="post" id="SalesAutomator-Conf" style="margin: 0; width: 100%; ">

                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label for="wp_sa_test"><?php _e('<a href="http://developer.paypal.com" target=_blank>Use PayPal Sandbox</a>'); ?></label></th>
                        <td>
                            <input id="wp_sa_test" name="wp_sa_test" class="regular-text" type="checkbox" value="checked" <?php echo $wp_sa_setting['wp_sa_test'] ;?> />
                            <span class="description">Testing purpose only.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_payment_email"><?php _e('Your PayPal Email Address'); ?></label>
                        </th>
                        <td>
                            <input type="text" id="wp_sa_payment_email" name="wp_sa_payment_email" class="regular-text" value="<?php echo $wp_sa_setting['wp_sa_payment_email']; ?>"  /><br />
                            <span class="description">Paypal email address through which you will receive payments.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_currency_code"><?php _e('Currency Code'); ?></label>
                        </th>
                        <td>
                            <input size="10%" id="wp_sa_currency_code" name="wp_sa_currency_code" type="text" class="regular-text" value="<?php echo $wp_sa_setting['wp_sa_currency_code']; ?>"  /><br />
                            <span class="description">The currency code for the payment amount.<br/>Example: USD</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><label for="wp_sa_display_recurring"><?php _e('Display Recurring Payment Option'); ?></label></th>
                        <td>
                            <input id="wp_sa_display_recurring" name="wp_sa_display_recurring" class="regular-text" type="checkbox" value="checked" <?php echo $wp_sa_setting['wp_sa_display_recurring']; ?> />
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_subscription_button_image"><?php _e('Subscription Button Image'); ?></label>
                        </th>
                        <td>
                            <input id="wp_sa_subscription_button_image" name="wp_sa_subscription_button_image" type="text" class="regular-text" value="<?php echo $wp_sa_setting['wp_sa_subscription_button_image']; ?>"  /><br />
                            <span class="description">Add the image URL to replace the default Subscription button. Leave it blank if you want to use default button.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_product_button_image"><?php _e('Buy Now Button Image'); ?></label>
                        </th>
                        <td>
                            <input id="wp_sa_product_button_image" name="wp_sa_product_button_image" type="text" class="regular-text" value="<?php echo $wp_sa_setting['wp_sa_product_button_image']; ?>"  /><br />
                            <span class="description">Add the image URL to replace the default Buy Now button. Leave it blank if you want to use default button.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <!-- this hidden field is added by radhe for saving salesautomator setting properly -->
                            <input type="hidden" name="wp_sa_actionn" value="wp_sa_submit" />
                            <p class="submit"><input type="submit" name="submit" value="<?php _e('Update Options &raquo;'); ?>" /></p>
                        </th>
                        <td>
                            <span class="description"></span>
                        </td>
                    </tr>
                </table>

                <h3><?php _e('Messages'); ?></h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_more_login"><?php _e('For Registered-user content'); ?></label>
                            <span class="description">HTML is allowed</span>
                        </th>
                        <td>
                            <textarea id="wp_sa_more_login" name="wp_sa_more_login" rows=5 cols=25 ><?php if($wp_sa_setting['wp_sa_more_login']!='') echo stripslashes($wp_sa_setting['wp_sa_more_login']); else echo 'Login/Register for more.'?></textarea>
                            <br />
                            <span class="description">This message will appear when content being accessed are for registered user. This message will follow login/register link.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_access_denied_msg"><?php _e('For Premium-user content'); ?></label>
                            <span class="description">HTML is allowed</span>
                        </th>
                        <td>
                            <textarea id="wp_sa_access_denied_msg" name="wp_sa_access_denied_msg" rows=5 cols=25 ><?php if($wp_sa_setting['wp_sa_access_denied_msg']!='') echo stripslashes( $wp_sa_setting['wp_sa_access_denied_msg']); else echo 'We are sorry. You do not have access to this area.'; ?></textarea>
                            <br />
                            <span class="description">This message will appear when content being accessed are for premium user. The message will follow link to subscription form.</span>
                        </td>
                    </tr>
                </table>
                <p class="submit"><input type="submit" name="submit" value="<?php _e('Update Options &raquo;'); ?>" /></p>
                <p style="height:25px"></p>

                <h3><?php _e('Other Configuration'); ?></h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_bypass"><?php _e('Privileged Group(s)'); ?></label><br />
                            <span class="description">Check the user groups that should always be given access to premium content</span>
                        </th>
                        <td style="text-transform:capitalize">
                        <?php
                        $rt_groups = $wp_sa_setting['wp_sa_closed_user_group'];
                        $str = '';
                        foreach( $wp_roles->role_objects as $key => $role ) {
                            foreach( $role->capabilities as $cap => $grant ) {
                                $role_user_level = array_reduce( array_keys( $role->capabilities ), array('WP_User', 'level_reduction'), 0);
                            }
                            
                            if($rt_groups[$role->name]) $checked="checked"; else $checked = '';

                            $str .= '<input type="checkbox" name="wp_sa_closed_user_group['.$role->name.']" value="'.$role->name.'"  '.$checked.' />'.$role->name.'<br />';
                        }
                        echo $str;
                        ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_login_redirection_url"><?php _e('Redirection URL'); ?></label>
                        </th>
                        <td>
                            <input class="regular-text" id="wp_sa_login_redirection_url" type="text" name="wp_sa_login_redirection_url" value="<?php echo $wp_sa_setting['wp_sa_login_redirection_url']; ?>"><br />
                            <span class="description">The URL to redirect to after user logs in.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_sub_payment_url"><?php _e('Membership Page URL'); ?></label>
                        </th>
                        <td>
                            <input class="regular-text" id="wp_sa_sub_payment_url" type="text" name="wp_sa_sub_payment_url" value="<?php echo $wp_sa_setting['wp_sa_sub_payment_url']; ?>"><br />
                            <span class="description">Paste the URL where you want to show membership options.<br />
                                Paste <code>[subscription_form]</code> shortcode in above page.
                                <br/>
                            </span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_reset_dnld_url"><?php _e('Reset Download URL'); ?></label>
                        </th>
                        <td>
                            <input class="regular-text" id="wp_sa_reset_dnld_url" name="wp_sa_reset_dnld_url" type="text" value="<?php echo $wp_sa_setting['wp_sa_reset_dnld_url']; ?>"><br />
                            <span class="description">Paste the URL where you want to show reset download option.<br />
                                Paste <code>[reset_download_link]</code> shortcode in above page.
                                <br/>
                            </span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_thank_you_page"><?php _e('Thank You Page URL'); ?></label>
                        </th>
                        <td>
                            <input class="regular-text" id="wp_sa_thank_you_page" name="wp_sa_thank_you_page" type="text" value="<?php echo $wp_sa_setting['wp_sa_thank_you_page']; ?>"><br />
                            <span class="description">Paste the URL where you want the customers to be directed after they have made successful payment.<br />
                                Paste <code>[thank_you_page]</code> shortcode in above page.
                                <br/>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th colspan="2">* After changing upload and temp directory path copy files from old directory to this new directory </th></tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_upload_dir_path"><?php _e('Upload directory path'); ?></label>
                        </th>
                        <td>                            
                            <input class="regular-text" id="wp_sa_upload_dir_path" name="wp_sa_upload_dir_path" type="text" value="<?php echo $wp_sa_setting['wp_sa_upload_dir_path'] ; ?>"><br />
                            <span class="description">(Note: directory path must end with '/' ) </span>
                        </td>
                    </tr>
                    
                    <tr valign="top">
                        <th scope="row">
                            <label for="wp_sa_keep_data"><?php _e(' Keep plugin/addon settings and data after plugin deleted?'); ?></label>
                        </th>
                        <td>
                            <input class="" id="wp_sa_keep_data" name="wp_sa_keep_data" type="checkbox" value="1" <?php if ($wp_sa_setting['wp_sa_keep_data']) echo 'checked'; ?>/>
                        </td>
                    </tr>
                </table>
                <p class="submit"><input type="submit" name="submit" value="<?php _e('Update Options &raquo;'); ?>" /></p>
                <p style="height:25px"></p>

                 <h3>Validity of Download Link</h3>
                <p><i><b>Note: </b>Download Link will expire after whichever limit first met from following limits.</i></p>
                <table class="form-table">
                        <tr valign="top">
                            <th scope="row"><b>Time Limit</b><br/><span class="description">(Download Link will expire after this duration.)</span></th>
                            <td>
                            <?php
                            $hours = $wp_sa_setting['rt_mail_option_hours'];
                            if ($hours != false) {
                                $hour = (int)($hours % 24);
                                $days = (int)( $hours / 24);
                            }
//                            if ($hour == '') {
//                                $days =$hour = 0;
//                            }
                            ?>
                                <input type="text" size="2" name="wp_sa_days" value="<?php echo $days; ?>" /> Days &nbsp;&nbsp;
                                <input type="text" size="2" name="wp_sa_hour" value="<?php echo $hour; ?>" /> Hours &nbsp;&nbsp;&nbsp;&nbsp; <i>(Minimum 10 Hours)</i>
                            </td>
                        <tr>
                            <th scope="row"><b>Max Count Limit</b><br/><span class="description">(Download Link will expire after this many number of downloads.)</span></th>
                            <td>
                                <input type="text"  size="4" name="wp_sa_max_count_limit" value="<?php echo ($wp_sa_setting['wp_sa_max_count_limit'] >= 1) ? $wp_sa_setting['wp_sa_max_count_limit'] : 1 ;?>"/> &nbsp;&nbsp;&nbsp;&nbsp; <i>(Minimum 1)</i>
                            </td>
                        </tr>
                </table>
                 <p class="submit"><input type="submit" name="submit" value="<?php _e('Update Options &raquo;'); ?>" /></p>
                <p style="height:25px"></p>
            </form>                   
            <h3><font color="#07AFE5">Receive Email Updates</font></h3>
                <p>To get email notification of new updates and releases, please enter your<br />
                first name and email below.</p>
                <div>
<!-- Add your autoresponder code below -->

Enter signup form code here

<!-- Add your autoresponder code above -->
</div>
        <?php
    }

/*
 * Description : It creates the specified directory and file, and Writes the given content into the file.
 * Its a function which accepts path of the directory, name of the file, and the content to be written into the file.
 * i.e creates a directory and file and writes given text into the file.
 */
    function create_file_and_directory ( $path, $fname, $content ) {
        // full path  of the file
        $file= $path.'/'.$fname;
        $dir = @opendir($path);
        // check for directory existance
        if(!is_dir($path))  {
            // create a directory
            if(@mkdir($path, 0755)) {
                // check for empty file name
                if(!empty ($fname)) { $f=1; } else $f=0;
            }
            else {
                return false;//return "Make Directory Using : sudo mkdir $path and Change Permissions Using Command : sudo chmod a+rw $path ";
            }
        }
        else {
            // check whether path is writable or not
            if(is_writable($path)) {
                if ($dir) {
                    // check whether file exists or writable
                    if(!file_exists($file) || !is_writable($file)) { @chmod($file, 0755); }
                    // check for empty file name
                    if(!empty ($fname)) { $f=1; } else $f=0;
                }
                else {
                    return false; // return "Change Permissions Using Command : sudo chmod a+rw $path";
                }
            }
            else {
                return false; //return "Change Permissions Using Command : sudo chmod a+rw $path";
            }
        }
        // Open a file in write mode and write the content into it.
        if($f==1) {
            $file_2 = @fopen($file,'w+');
            @fwrite($file_2, $content);
            return "true";
        }
        else return "false";
        @closedir($dir);
    }
}
?>