Completed
Branch models-cleanup/model-relations (db5ca7)
by
unknown
13:03 queued 08:35
created
core/db_models/helpers/EE_Model_Query_Info_Carrier.php 2 patches
Indentation   +90 added lines, -90 removed lines patch added patch discarded remove patch
@@ -14,141 +14,141 @@  discard block
 block discarded – undo
14 14
    /**
15 15
     * @var string SQL for performing joins (Eg, "INNER JOIN blah ON blah=blah INNER JOIN FOO ON foo=foo...")
16 16
     */
17
-    private $_join_sql;
17
+	private $_join_sql;
18 18
    /**
19 19
     *
20 20
     * @var array stating all the models that have been included thus far,so we don't get duplicates.
21 21
     * Keys are the model relation chains to them from the queried model
22 22
     * (eg, "Registration.Transaction.Payment"), and valuesare model names (eg "Payment")
23 23
     */
24
-    private $_models_included;
24
+	private $_models_included;
25 25
    
26 26
    /**
27 27
     * After we've acquired all the data types, we can create this sql.
28 28
     * @var string
29 29
     */
30
-    private $_where_sql;
30
+	private $_where_sql;
31 31
    /**
32 32
     * Full join sql. Eg, in a select query, that's everything after the "FROM", and before the "WHERE", so it includes
33 33
     * the declaration of the main model's tables, and then appends all the joining sql to other models
34 34
     * @var string
35 35
     */
36
-    private $_main_join_sql;
36
+	private $_main_join_sql;
37 37
    
38 38
    
39
-    private $_limit_sql;
39
+	private $_limit_sql;
40 40
    
41
-    private $_order_by_sql;
41
+	private $_order_by_sql;
42 42
    
43
-    private $_having_sql;
43
+	private $_having_sql;
44 44
    
45
-    private $_group_by_sql;
45
+	private $_group_by_sql;
46 46
    
47
-    public function set_limit_sql($limit_sql)
48
-    {
49
-        $this->_limit_sql = $limit_sql;
50
-    }
47
+	public function set_limit_sql($limit_sql)
48
+	{
49
+		$this->_limit_sql = $limit_sql;
50
+	}
51 51
     
52
-    public function set_order_by_sql($order_by_sql)
53
-    {
54
-        $this->_order_by_sql = $order_by_sql;
55
-    }
56
-    public function set_group_by_sql($group_by_sql)
57
-    {
58
-        $this->_group_by_sql = $group_by_sql;
59
-    }
60
-    public function set_having_sql($having_sql)
61
-    {
62
-        $this->_having_sql = $having_sql;
63
-    }
64
-    public function get_limit_sql()
65
-    {
66
-        return $this->_limit_sql;
67
-    }
68
-    public function get_order_by_sql()
69
-    {
70
-        return $this->_order_by_sql;
71
-    }
72
-    public function get_group_by_sql()
73
-    {
74
-        return $this->_group_by_sql;
75
-    }
76
-    public function get_having_sql()
77
-    {
78
-        return $this->_having_sql;
79
-    }
52
+	public function set_order_by_sql($order_by_sql)
53
+	{
54
+		$this->_order_by_sql = $order_by_sql;
55
+	}
56
+	public function set_group_by_sql($group_by_sql)
57
+	{
58
+		$this->_group_by_sql = $group_by_sql;
59
+	}
60
+	public function set_having_sql($having_sql)
61
+	{
62
+		$this->_having_sql = $having_sql;
63
+	}
64
+	public function get_limit_sql()
65
+	{
66
+		return $this->_limit_sql;
67
+	}
68
+	public function get_order_by_sql()
69
+	{
70
+		return $this->_order_by_sql;
71
+	}
72
+	public function get_group_by_sql()
73
+	{
74
+		return $this->_group_by_sql;
75
+	}
76
+	public function get_having_sql()
77
+	{
78
+		return $this->_having_sql;
79
+	}
80 80
    /**
81 81
     *
82 82
     * @param type $model_included_name
83 83
     * @param type $join_sql
84 84
     * @param type $data_types
85 85
     */
86
-    public function __construct($model_included_name = array(), $join_sql = '')
87
-    {
88
-        $this->_models_included = $model_included_name;
89
-        $this->_join_sql = $join_sql;
90
-    }
86
+	public function __construct($model_included_name = array(), $join_sql = '')
87
+	{
88
+		$this->_models_included = $model_included_name;
89
+		$this->_join_sql = $join_sql;
90
+	}
91 91
    
92 92
    /**
93 93
     * Merges info from the other EEM_Related_Model_Info_Carrier into this one.
94 94
     * @param EE_Model_Query_Info_Carrier $other_model_query_info_carrier
95 95
     */
96
-    public function merge($other_model_query_info_carrier)
97
-    {
98
-        if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) {
99
-            $model_included_on_other_join_sql_and_data_types_carrier =  $other_model_query_info_carrier->get_model_names_included();
100
-            $this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier);
101
-            $this->_join_sql .= $other_model_query_info_carrier->_join_sql;
102
-        }
103
-        // otherwise don't merge our data.
104
-        // yes, this means that we must immediately merge any model data into our grand list
105
-        // as soon as we get some from ONE model, or else we could reject a EEM_Related_Model_Info_Carrier
106
-        // which is carrying info from two models WHERE one is already included but the other is NOT
107
-    }
96
+	public function merge($other_model_query_info_carrier)
97
+	{
98
+		if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) {
99
+			$model_included_on_other_join_sql_and_data_types_carrier =  $other_model_query_info_carrier->get_model_names_included();
100
+			$this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier);
101
+			$this->_join_sql .= $other_model_query_info_carrier->_join_sql;
102
+		}
103
+		// otherwise don't merge our data.
104
+		// yes, this means that we must immediately merge any model data into our grand list
105
+		// as soon as we get some from ONE model, or else we could reject a EEM_Related_Model_Info_Carrier
106
+		// which is carrying info from two models WHERE one is already included but the other is NOT
107
+	}
108 108
    /**
109 109
     * Checks whether or not we have already included all the models mentione din $model_names on the query info varrier
110 110
     * @param array $model_names just like EE_MOdel_QUery_Info_Carrier::_models_included: keys are model chain paths, values are the model names only
111 111
     * @return boolean
112 112
     */
113
-    protected function _have_already_included_one_of_these_models($model_names)
114
-    {
115
-        foreach ($this->_models_included as $model_relation_path => $model_included) {
116
-            if (array_key_exists($model_relation_path, $model_names)) {
117
-                return true;
118
-            }
119
-        }
120
-        return false;
121
-    }
113
+	protected function _have_already_included_one_of_these_models($model_names)
114
+	{
115
+		foreach ($this->_models_included as $model_relation_path => $model_included) {
116
+			if (array_key_exists($model_relation_path, $model_names)) {
117
+				return true;
118
+			}
119
+		}
120
+		return false;
121
+	}
122 122
    /**
123 123
     * Array keys are model names, values are "model relation paths". See EE_Model_Query_Info_Carrier::_models_included for details
124 124
     * @return array like EE_Model_Query_Info_Carrier::_models_included
125 125
     */
126
-    public function get_model_names_included()
127
-    {
128
-        return $this->_models_included;
129
-    }
126
+	public function get_model_names_included()
127
+	{
128
+		return $this->_models_included;
129
+	}
130 130
    /**
131 131
     * sets the $where_sql for later use from client code
132 132
     * @param string $where_sql
133 133
     */
134
-    public function set_where_sql($where_sql)
135
-    {
136
-        $this->_where_sql = $where_sql;
137
-    }
138
-    public function get_where_sql()
139
-    {
140
-        return $this->_where_sql;
141
-    }
134
+	public function set_where_sql($where_sql)
135
+	{
136
+		$this->_where_sql = $where_sql;
137
+	}
138
+	public function get_where_sql()
139
+	{
140
+		return $this->_where_sql;
141
+	}
142 142
 
143 143
    /**
144 144
     * Gets the SQL for joining the main model to other models involves in the query, which was set earlier on
145 145
     * the EE_Model_Query_info_Carrier by calling set_main_model_join_sql()
146 146
     * @return string
147 147
     */
148
-    public function get_main_model_join_sql()
149
-    {
150
-        return $this->_main_join_sql;
151
-    }
148
+	public function get_main_model_join_sql()
149
+	{
150
+		return $this->_main_join_sql;
151
+	}
152 152
 
153 153
 
154 154
    /**
@@ -158,12 +158,12 @@  discard block
 block discarded – undo
158 158
     * after the FROM and before the WHERE.)
159 159
     * @param string $join_sql
160 160
     */
161
-    public function set_main_model_join_sql($join_sql)
162
-    {
163
-        $this->_main_join_sql = $join_sql;
164
-    }
165
-    public function get_full_join_sql()
166
-    {
167
-        return $this->_main_join_sql . $this->_join_sql;
168
-    }
161
+	public function set_main_model_join_sql($join_sql)
162
+	{
163
+		$this->_main_join_sql = $join_sql;
164
+	}
165
+	public function get_full_join_sql()
166
+	{
167
+		return $this->_main_join_sql . $this->_join_sql;
168
+	}
169 169
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
     public function merge($other_model_query_info_carrier)
97 97
     {
98 98
         if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) {
99
-            $model_included_on_other_join_sql_and_data_types_carrier =  $other_model_query_info_carrier->get_model_names_included();
99
+            $model_included_on_other_join_sql_and_data_types_carrier = $other_model_query_info_carrier->get_model_names_included();
100 100
             $this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier);
101 101
             $this->_join_sql .= $other_model_query_info_carrier->_join_sql;
102 102
         }
@@ -164,6 +164,6 @@  discard block
 block discarded – undo
164 164
     }
165 165
     public function get_full_join_sql()
166 166
     {
167
-        return $this->_main_join_sql . $this->_join_sql;
167
+        return $this->_main_join_sql.$this->_join_sql;
168 168
     }
169 169
 }
Please login to merge, or discard this patch.
core/EE_Cart.core.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -65,14 +65,14 @@  discard block
 block discarded – undo
65 65
             self::$_instance = new self($grand_total, $session);
66 66
         }
67 67
         // or maybe retrieve an existing one ?
68
-        if (! self::$_instance instanceof EE_Cart) {
68
+        if ( ! self::$_instance instanceof EE_Cart) {
69 69
             // try getting the cart out of the session
70 70
             $saved_cart = $session instanceof EE_Session ? $session->cart() : null;
71 71
             self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session);
72 72
             unset($saved_cart);
73 73
         }
74 74
         // verify that cart is ok and grand total line item exists
75
-        if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) {
75
+        if ( ! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) {
76 76
             self::$_instance = new self($grand_total, $session);
77 77
         }
78 78
         self::$_instance->get_grand_total();
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
      */
125 125
     public function session()
126 126
     {
127
-        if (! $this->_session instanceof EE_Session) {
127
+        if ( ! $this->_session instanceof EE_Session) {
128 128
             $this->set_session();
129 129
         }
130 130
         return $this->_session;
@@ -392,7 +392,7 @@  discard block
 block discarded – undo
392 392
 
393 393
     public function __wakeup()
394 394
     {
395
-        if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) {
395
+        if ( ! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) {
396 396
             // $this->_grand_total is actually just an ID, so use it to get the object from the db
397 397
             $this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total);
398 398
         }
Please login to merge, or discard this patch.
Indentation   +393 added lines, -393 removed lines patch added patch discarded remove patch
@@ -17,397 +17,397 @@
 block discarded – undo
17 17
 class EE_Cart implements ResettableInterface
18 18
 {
19 19
 
20
-    /**
21
-     * instance of the EE_Cart object
22
-     *
23
-     * @access    private
24
-     * @var EE_Cart $_instance
25
-     */
26
-    private static $_instance;
27
-
28
-    /**
29
-     * instance of the EE_Session object
30
-     *
31
-     * @access    protected
32
-     * @var EE_Session $_session
33
-     */
34
-    protected $_session;
35
-
36
-    /**
37
-     * The total Line item which comprises all the children line-item subtotals,
38
-     * which in turn each have their line items.
39
-     * Typically, the line item structure will look like:
40
-     * grand total
41
-     * -tickets-sub-total
42
-     * --ticket1
43
-     * --ticket2
44
-     * --...
45
-     * -taxes-sub-total
46
-     * --tax1
47
-     * --tax2
48
-     *
49
-     * @var EE_Line_Item
50
-     */
51
-    private $_grand_total;
52
-
53
-
54
-    /**
55
-     * @singleton method used to instantiate class object
56
-     * @access    public
57
-     * @param EE_Line_Item $grand_total
58
-     * @param EE_Session   $session
59
-     * @return \EE_Cart
60
-     * @throws \EE_Error
61
-     */
62
-    public static function instance(EE_Line_Item $grand_total = null, EE_Session $session = null)
63
-    {
64
-        if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) {
65
-            self::$_instance = new self($grand_total, $session);
66
-        }
67
-        // or maybe retrieve an existing one ?
68
-        if (! self::$_instance instanceof EE_Cart) {
69
-            // try getting the cart out of the session
70
-            $saved_cart = $session instanceof EE_Session ? $session->cart() : null;
71
-            self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session);
72
-            unset($saved_cart);
73
-        }
74
-        // verify that cart is ok and grand total line item exists
75
-        if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) {
76
-            self::$_instance = new self($grand_total, $session);
77
-        }
78
-        self::$_instance->get_grand_total();
79
-        // once everything is all said and done, save the cart to the EE_Session
80
-        add_action('shutdown', array(self::$_instance, 'save_cart'), 90);
81
-        return self::$_instance;
82
-    }
83
-
84
-
85
-    /**
86
-     * private constructor to prevent direct creation
87
-     *
88
-     * @Constructor
89
-     * @access private
90
-     * @param EE_Line_Item $grand_total
91
-     * @param EE_Session   $session
92
-     */
93
-    private function __construct(EE_Line_Item $grand_total = null, EE_Session $session = null)
94
-    {
95
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
96
-        $this->set_session($session);
97
-        if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) {
98
-            $this->set_grand_total_line_item($grand_total);
99
-        }
100
-    }
101
-
102
-
103
-    /**
104
-     * Resets the cart completely (whereas empty_cart
105
-     *
106
-     * @param EE_Line_Item $grand_total
107
-     * @param EE_Session   $session
108
-     * @return EE_Cart
109
-     * @throws \EE_Error
110
-     */
111
-    public static function reset(EE_Line_Item $grand_total = null, EE_Session $session = null)
112
-    {
113
-        remove_action('shutdown', array(self::$_instance, 'save_cart'), 90);
114
-        if ($session instanceof EE_Session) {
115
-            $session->reset_cart();
116
-        }
117
-        self::$_instance = null;
118
-        return self::instance($grand_total, $session);
119
-    }
120
-
121
-
122
-    /**
123
-     * @return \EE_Session
124
-     */
125
-    public function session()
126
-    {
127
-        if (! $this->_session instanceof EE_Session) {
128
-            $this->set_session();
129
-        }
130
-        return $this->_session;
131
-    }
132
-
133
-
134
-    /**
135
-     * @param EE_Session $session
136
-     */
137
-    public function set_session(EE_Session $session = null)
138
-    {
139
-        $this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core('Session');
140
-    }
141
-
142
-
143
-    /**
144
-     * Sets the cart to match the line item. Especially handy for loading an old cart where you
145
-     *  know the grand total line item on it
146
-     *
147
-     * @param EE_Line_Item $line_item
148
-     */
149
-    public function set_grand_total_line_item(EE_Line_Item $line_item)
150
-    {
151
-        $this->_grand_total = $line_item;
152
-    }
153
-
154
-
155
-    /**
156
-     * get_cart_from_reg_url_link
157
-     *
158
-     * @access public
159
-     * @param EE_Transaction $transaction
160
-     * @param EE_Session     $session
161
-     * @return \EE_Cart
162
-     * @throws \EE_Error
163
-     */
164
-    public static function get_cart_from_txn(EE_Transaction $transaction, EE_Session $session = null)
165
-    {
166
-        $grand_total = $transaction->total_line_item();
167
-        $grand_total->get_items();
168
-        $grand_total->tax_descendants();
169
-        return EE_Cart::instance($grand_total, $session);
170
-    }
171
-
172
-
173
-    /**
174
-     * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items
175
-     *
176
-     * @return EE_Line_Item
177
-     * @throws \EE_Error
178
-     */
179
-    private function _create_grand_total()
180
-    {
181
-        $this->_grand_total = EEH_Line_Item::create_total_line_item();
182
-        return $this->_grand_total;
183
-    }
184
-
185
-
186
-    /**
187
-     * Gets all the line items of object type Ticket
188
-     *
189
-     * @access public
190
-     * @return \EE_Line_Item[]
191
-     */
192
-    public function get_tickets()
193
-    {
194
-        if ($this->_grand_total === null) {
195
-            return array();
196
-        }
197
-        return EEH_Line_Item::get_ticket_line_items($this->_grand_total);
198
-    }
199
-
200
-
201
-    /**
202
-     * returns the total quantity of tickets in the cart
203
-     *
204
-     * @access public
205
-     * @return int
206
-     * @throws \EE_Error
207
-     */
208
-    public function all_ticket_quantity_count()
209
-    {
210
-        $tickets = $this->get_tickets();
211
-        if (empty($tickets)) {
212
-            return 0;
213
-        }
214
-        $count = 0;
215
-        foreach ($tickets as $ticket) {
216
-            $count += $ticket->get('LIN_quantity');
217
-        }
218
-        return $count;
219
-    }
220
-
221
-
222
-    /**
223
-     * Gets all the tax line items
224
-     *
225
-     * @return \EE_Line_Item[]
226
-     * @throws \EE_Error
227
-     */
228
-    public function get_taxes()
229
-    {
230
-        return EEH_Line_Item::get_taxes_subtotal($this->_grand_total)->children();
231
-    }
232
-
233
-
234
-    /**
235
-     * Gets the total line item (which is a parent of all other line items) on this cart
236
-     *
237
-     * @return EE_Line_Item
238
-     * @throws \EE_Error
239
-     */
240
-    public function get_grand_total()
241
-    {
242
-        return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total();
243
-    }
244
-
245
-
246
-    /**
247
-     * @process items for adding to cart
248
-     * @access  public
249
-     * @param EE_Ticket $ticket
250
-     * @param int       $qty
251
-     * @return TRUE on success, FALSE on fail
252
-     * @throws \EE_Error
253
-     */
254
-    public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1)
255
-    {
256
-        EEH_Line_Item::add_ticket_purchase($this->get_grand_total(), $ticket, $qty);
257
-        return $this->save_cart() ? true : false;
258
-    }
259
-
260
-
261
-    /**
262
-     * get_cart_total_before_tax
263
-     *
264
-     * @access public
265
-     * @return float
266
-     * @throws \EE_Error
267
-     */
268
-    public function get_cart_total_before_tax()
269
-    {
270
-        return $this->get_grand_total()->recalculate_pre_tax_total();
271
-    }
272
-
273
-
274
-    /**
275
-     * gets the total amount of tax paid for items in this cart
276
-     *
277
-     * @access public
278
-     * @return float
279
-     * @throws \EE_Error
280
-     */
281
-    public function get_applied_taxes()
282
-    {
283
-        return EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
284
-    }
285
-
286
-
287
-    /**
288
-     * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
289
-     *
290
-     * @access public
291
-     * @return float
292
-     * @throws \EE_Error
293
-     */
294
-    public function get_cart_grand_total()
295
-    {
296
-        EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
297
-        return $this->get_grand_total()->total();
298
-    }
299
-
300
-
301
-    /**
302
-     * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
303
-     *
304
-     * @access public
305
-     * @return float
306
-     * @throws \EE_Error
307
-     */
308
-    public function recalculate_all_cart_totals()
309
-    {
310
-        $pre_tax_total = $this->get_cart_total_before_tax();
311
-        $taxes_total = EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
312
-        $this->_grand_total->set_total($pre_tax_total + $taxes_total);
313
-        $this->_grand_total->save_this_and_descendants_to_txn();
314
-        return $this->get_grand_total()->total();
315
-    }
316
-
317
-
318
-    /**
319
-     * deletes an item from the cart
320
-     *
321
-     * @access public
322
-     * @param array|bool|string $line_item_codes
323
-     * @return int on success, FALSE on fail
324
-     * @throws \EE_Error
325
-     */
326
-    public function delete_items($line_item_codes = false)
327
-    {
328
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
329
-        return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes);
330
-    }
331
-
332
-
333
-    /**
334
-     * @remove ALL items from cart and zero ALL totals
335
-     * @access public
336
-     * @return bool
337
-     * @throws \EE_Error
338
-     */
339
-    public function empty_cart()
340
-    {
341
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
342
-        $this->_grand_total = $this->_create_grand_total();
343
-        return $this->save_cart(true);
344
-    }
345
-
346
-
347
-    /**
348
-     * @remove ALL items from cart and delete total as well
349
-     * @access public
350
-     * @return bool
351
-     * @throws \EE_Error
352
-     */
353
-    public function delete_cart()
354
-    {
355
-        if ($this->_grand_total instanceof EE_Line_Item) {
356
-            $deleted = EEH_Line_Item::delete_all_child_items($this->_grand_total);
357
-            if ($deleted) {
358
-                $deleted += $this->_grand_total->delete();
359
-                $this->_grand_total = null;
360
-                return true;
361
-            }
362
-        }
363
-        return false;
364
-    }
365
-
366
-
367
-    /**
368
-     * @save   cart to session
369
-     * @access public
370
-     * @param bool $apply_taxes
371
-     * @return TRUE on success, FALSE on fail
372
-     * @throws \EE_Error
373
-     */
374
-    public function save_cart($apply_taxes = true)
375
-    {
376
-        if ($apply_taxes && $this->_grand_total instanceof EE_Line_Item) {
377
-            EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
378
-            // make sure we don't cache the transaction because it can get stale
379
-            if (
380
-                $this->_grand_total->get_one_from_cache('Transaction') instanceof EE_Transaction
381
-                && $this->_grand_total->get_one_from_cache('Transaction')->ID()
382
-            ) {
383
-                $this->_grand_total->clear_cache('Transaction', null, true);
384
-            }
385
-        }
386
-        if ($this->session() instanceof EE_Session) {
387
-            return $this->session()->set_cart($this);
388
-        } else {
389
-            return false;
390
-        }
391
-    }
392
-
393
-
394
-    public function __wakeup()
395
-    {
396
-        if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) {
397
-            // $this->_grand_total is actually just an ID, so use it to get the object from the db
398
-            $this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total);
399
-        }
400
-    }
401
-
402
-
403
-    /**
404
-     * @return array
405
-     */
406
-    public function __sleep()
407
-    {
408
-        if ($this->_grand_total instanceof EE_Line_Item && $this->_grand_total->ID()) {
409
-            $this->_grand_total = $this->_grand_total->ID();
410
-        }
411
-        return array('_grand_total');
412
-    }
20
+	/**
21
+	 * instance of the EE_Cart object
22
+	 *
23
+	 * @access    private
24
+	 * @var EE_Cart $_instance
25
+	 */
26
+	private static $_instance;
27
+
28
+	/**
29
+	 * instance of the EE_Session object
30
+	 *
31
+	 * @access    protected
32
+	 * @var EE_Session $_session
33
+	 */
34
+	protected $_session;
35
+
36
+	/**
37
+	 * The total Line item which comprises all the children line-item subtotals,
38
+	 * which in turn each have their line items.
39
+	 * Typically, the line item structure will look like:
40
+	 * grand total
41
+	 * -tickets-sub-total
42
+	 * --ticket1
43
+	 * --ticket2
44
+	 * --...
45
+	 * -taxes-sub-total
46
+	 * --tax1
47
+	 * --tax2
48
+	 *
49
+	 * @var EE_Line_Item
50
+	 */
51
+	private $_grand_total;
52
+
53
+
54
+	/**
55
+	 * @singleton method used to instantiate class object
56
+	 * @access    public
57
+	 * @param EE_Line_Item $grand_total
58
+	 * @param EE_Session   $session
59
+	 * @return \EE_Cart
60
+	 * @throws \EE_Error
61
+	 */
62
+	public static function instance(EE_Line_Item $grand_total = null, EE_Session $session = null)
63
+	{
64
+		if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) {
65
+			self::$_instance = new self($grand_total, $session);
66
+		}
67
+		// or maybe retrieve an existing one ?
68
+		if (! self::$_instance instanceof EE_Cart) {
69
+			// try getting the cart out of the session
70
+			$saved_cart = $session instanceof EE_Session ? $session->cart() : null;
71
+			self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session);
72
+			unset($saved_cart);
73
+		}
74
+		// verify that cart is ok and grand total line item exists
75
+		if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) {
76
+			self::$_instance = new self($grand_total, $session);
77
+		}
78
+		self::$_instance->get_grand_total();
79
+		// once everything is all said and done, save the cart to the EE_Session
80
+		add_action('shutdown', array(self::$_instance, 'save_cart'), 90);
81
+		return self::$_instance;
82
+	}
83
+
84
+
85
+	/**
86
+	 * private constructor to prevent direct creation
87
+	 *
88
+	 * @Constructor
89
+	 * @access private
90
+	 * @param EE_Line_Item $grand_total
91
+	 * @param EE_Session   $session
92
+	 */
93
+	private function __construct(EE_Line_Item $grand_total = null, EE_Session $session = null)
94
+	{
95
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
96
+		$this->set_session($session);
97
+		if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) {
98
+			$this->set_grand_total_line_item($grand_total);
99
+		}
100
+	}
101
+
102
+
103
+	/**
104
+	 * Resets the cart completely (whereas empty_cart
105
+	 *
106
+	 * @param EE_Line_Item $grand_total
107
+	 * @param EE_Session   $session
108
+	 * @return EE_Cart
109
+	 * @throws \EE_Error
110
+	 */
111
+	public static function reset(EE_Line_Item $grand_total = null, EE_Session $session = null)
112
+	{
113
+		remove_action('shutdown', array(self::$_instance, 'save_cart'), 90);
114
+		if ($session instanceof EE_Session) {
115
+			$session->reset_cart();
116
+		}
117
+		self::$_instance = null;
118
+		return self::instance($grand_total, $session);
119
+	}
120
+
121
+
122
+	/**
123
+	 * @return \EE_Session
124
+	 */
125
+	public function session()
126
+	{
127
+		if (! $this->_session instanceof EE_Session) {
128
+			$this->set_session();
129
+		}
130
+		return $this->_session;
131
+	}
132
+
133
+
134
+	/**
135
+	 * @param EE_Session $session
136
+	 */
137
+	public function set_session(EE_Session $session = null)
138
+	{
139
+		$this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core('Session');
140
+	}
141
+
142
+
143
+	/**
144
+	 * Sets the cart to match the line item. Especially handy for loading an old cart where you
145
+	 *  know the grand total line item on it
146
+	 *
147
+	 * @param EE_Line_Item $line_item
148
+	 */
149
+	public function set_grand_total_line_item(EE_Line_Item $line_item)
150
+	{
151
+		$this->_grand_total = $line_item;
152
+	}
153
+
154
+
155
+	/**
156
+	 * get_cart_from_reg_url_link
157
+	 *
158
+	 * @access public
159
+	 * @param EE_Transaction $transaction
160
+	 * @param EE_Session     $session
161
+	 * @return \EE_Cart
162
+	 * @throws \EE_Error
163
+	 */
164
+	public static function get_cart_from_txn(EE_Transaction $transaction, EE_Session $session = null)
165
+	{
166
+		$grand_total = $transaction->total_line_item();
167
+		$grand_total->get_items();
168
+		$grand_total->tax_descendants();
169
+		return EE_Cart::instance($grand_total, $session);
170
+	}
171
+
172
+
173
+	/**
174
+	 * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items
175
+	 *
176
+	 * @return EE_Line_Item
177
+	 * @throws \EE_Error
178
+	 */
179
+	private function _create_grand_total()
180
+	{
181
+		$this->_grand_total = EEH_Line_Item::create_total_line_item();
182
+		return $this->_grand_total;
183
+	}
184
+
185
+
186
+	/**
187
+	 * Gets all the line items of object type Ticket
188
+	 *
189
+	 * @access public
190
+	 * @return \EE_Line_Item[]
191
+	 */
192
+	public function get_tickets()
193
+	{
194
+		if ($this->_grand_total === null) {
195
+			return array();
196
+		}
197
+		return EEH_Line_Item::get_ticket_line_items($this->_grand_total);
198
+	}
199
+
200
+
201
+	/**
202
+	 * returns the total quantity of tickets in the cart
203
+	 *
204
+	 * @access public
205
+	 * @return int
206
+	 * @throws \EE_Error
207
+	 */
208
+	public function all_ticket_quantity_count()
209
+	{
210
+		$tickets = $this->get_tickets();
211
+		if (empty($tickets)) {
212
+			return 0;
213
+		}
214
+		$count = 0;
215
+		foreach ($tickets as $ticket) {
216
+			$count += $ticket->get('LIN_quantity');
217
+		}
218
+		return $count;
219
+	}
220
+
221
+
222
+	/**
223
+	 * Gets all the tax line items
224
+	 *
225
+	 * @return \EE_Line_Item[]
226
+	 * @throws \EE_Error
227
+	 */
228
+	public function get_taxes()
229
+	{
230
+		return EEH_Line_Item::get_taxes_subtotal($this->_grand_total)->children();
231
+	}
232
+
233
+
234
+	/**
235
+	 * Gets the total line item (which is a parent of all other line items) on this cart
236
+	 *
237
+	 * @return EE_Line_Item
238
+	 * @throws \EE_Error
239
+	 */
240
+	public function get_grand_total()
241
+	{
242
+		return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total();
243
+	}
244
+
245
+
246
+	/**
247
+	 * @process items for adding to cart
248
+	 * @access  public
249
+	 * @param EE_Ticket $ticket
250
+	 * @param int       $qty
251
+	 * @return TRUE on success, FALSE on fail
252
+	 * @throws \EE_Error
253
+	 */
254
+	public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1)
255
+	{
256
+		EEH_Line_Item::add_ticket_purchase($this->get_grand_total(), $ticket, $qty);
257
+		return $this->save_cart() ? true : false;
258
+	}
259
+
260
+
261
+	/**
262
+	 * get_cart_total_before_tax
263
+	 *
264
+	 * @access public
265
+	 * @return float
266
+	 * @throws \EE_Error
267
+	 */
268
+	public function get_cart_total_before_tax()
269
+	{
270
+		return $this->get_grand_total()->recalculate_pre_tax_total();
271
+	}
272
+
273
+
274
+	/**
275
+	 * gets the total amount of tax paid for items in this cart
276
+	 *
277
+	 * @access public
278
+	 * @return float
279
+	 * @throws \EE_Error
280
+	 */
281
+	public function get_applied_taxes()
282
+	{
283
+		return EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
284
+	}
285
+
286
+
287
+	/**
288
+	 * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
289
+	 *
290
+	 * @access public
291
+	 * @return float
292
+	 * @throws \EE_Error
293
+	 */
294
+	public function get_cart_grand_total()
295
+	{
296
+		EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
297
+		return $this->get_grand_total()->total();
298
+	}
299
+
300
+
301
+	/**
302
+	 * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers
303
+	 *
304
+	 * @access public
305
+	 * @return float
306
+	 * @throws \EE_Error
307
+	 */
308
+	public function recalculate_all_cart_totals()
309
+	{
310
+		$pre_tax_total = $this->get_cart_total_before_tax();
311
+		$taxes_total = EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
312
+		$this->_grand_total->set_total($pre_tax_total + $taxes_total);
313
+		$this->_grand_total->save_this_and_descendants_to_txn();
314
+		return $this->get_grand_total()->total();
315
+	}
316
+
317
+
318
+	/**
319
+	 * deletes an item from the cart
320
+	 *
321
+	 * @access public
322
+	 * @param array|bool|string $line_item_codes
323
+	 * @return int on success, FALSE on fail
324
+	 * @throws \EE_Error
325
+	 */
326
+	public function delete_items($line_item_codes = false)
327
+	{
328
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
329
+		return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes);
330
+	}
331
+
332
+
333
+	/**
334
+	 * @remove ALL items from cart and zero ALL totals
335
+	 * @access public
336
+	 * @return bool
337
+	 * @throws \EE_Error
338
+	 */
339
+	public function empty_cart()
340
+	{
341
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
342
+		$this->_grand_total = $this->_create_grand_total();
343
+		return $this->save_cart(true);
344
+	}
345
+
346
+
347
+	/**
348
+	 * @remove ALL items from cart and delete total as well
349
+	 * @access public
350
+	 * @return bool
351
+	 * @throws \EE_Error
352
+	 */
353
+	public function delete_cart()
354
+	{
355
+		if ($this->_grand_total instanceof EE_Line_Item) {
356
+			$deleted = EEH_Line_Item::delete_all_child_items($this->_grand_total);
357
+			if ($deleted) {
358
+				$deleted += $this->_grand_total->delete();
359
+				$this->_grand_total = null;
360
+				return true;
361
+			}
362
+		}
363
+		return false;
364
+	}
365
+
366
+
367
+	/**
368
+	 * @save   cart to session
369
+	 * @access public
370
+	 * @param bool $apply_taxes
371
+	 * @return TRUE on success, FALSE on fail
372
+	 * @throws \EE_Error
373
+	 */
374
+	public function save_cart($apply_taxes = true)
375
+	{
376
+		if ($apply_taxes && $this->_grand_total instanceof EE_Line_Item) {
377
+			EEH_Line_Item::ensure_taxes_applied($this->_grand_total);
378
+			// make sure we don't cache the transaction because it can get stale
379
+			if (
380
+				$this->_grand_total->get_one_from_cache('Transaction') instanceof EE_Transaction
381
+				&& $this->_grand_total->get_one_from_cache('Transaction')->ID()
382
+			) {
383
+				$this->_grand_total->clear_cache('Transaction', null, true);
384
+			}
385
+		}
386
+		if ($this->session() instanceof EE_Session) {
387
+			return $this->session()->set_cart($this);
388
+		} else {
389
+			return false;
390
+		}
391
+	}
392
+
393
+
394
+	public function __wakeup()
395
+	{
396
+		if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) {
397
+			// $this->_grand_total is actually just an ID, so use it to get the object from the db
398
+			$this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total);
399
+		}
400
+	}
401
+
402
+
403
+	/**
404
+	 * @return array
405
+	 */
406
+	public function __sleep()
407
+	{
408
+		if ($this->_grand_total instanceof EE_Line_Item && $this->_grand_total->ID()) {
409
+			$this->_grand_total = $this->_grand_total->ID();
410
+		}
411
+		return array('_grand_total');
412
+	}
413 413
 }
Please login to merge, or discard this patch.
core/middleware/EE_Detect_Login.core.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -13,27 +13,27 @@
 block discarded – undo
13 13
 {
14 14
 
15 15
 
16
-    /**
17
-     * @deprecated
18
-     * @param    EE_Request  $request
19
-     * @param    EE_Response $response
20
-     * @return    EE_Response
21
-     */
22
-    public function handle_request(EE_Request $request, EE_Response $response)
23
-    {
24
-        EE_Error::doing_it_wrong(
25
-            __METHOD__,
26
-            sprintf(
27
-                esc_html__(
28
-                    'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
29
-                    'event_espresso'
30
-                ),
31
-                'EventEspresso\core\services\request\middleware\DetectLogin',
32
-                '\core\services\request',
33
-                'EventEspresso\core\services\request'
34
-            ),
35
-            '4.9.52'
36
-        );
37
-        return $response;
38
-    }
16
+	/**
17
+	 * @deprecated
18
+	 * @param    EE_Request  $request
19
+	 * @param    EE_Response $response
20
+	 * @return    EE_Response
21
+	 */
22
+	public function handle_request(EE_Request $request, EE_Response $response)
23
+	{
24
+		EE_Error::doing_it_wrong(
25
+			__METHOD__,
26
+			sprintf(
27
+				esc_html__(
28
+					'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
29
+					'event_espresso'
30
+				),
31
+				'EventEspresso\core\services\request\middleware\DetectLogin',
32
+				'\core\services\request',
33
+				'EventEspresso\core\services\request'
34
+			),
35
+			'4.9.52'
36
+		);
37
+		return $response;
38
+	}
39 39
 }
Please login to merge, or discard this patch.
core/middleware/EE_Recommended_Versions.core.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -17,75 +17,75 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
 
20
-    /**
21
-     * @deprecated
22
-     * @param EE_Request  $request
23
-     * @param EE_Response $response
24
-     * @return EE_Response
25
-     */
26
-    public function handle_request(EE_Request $request, EE_Response $response)
27
-    {
28
-        EE_Error::doing_it_wrong(
29
-            __METHOD__,
30
-            sprintf(
31
-                esc_html__(
32
-                    'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
33
-                    'event_espresso'
34
-                ),
35
-                'EventEspresso\core\services\request\middleware\RecommendedVersions',
36
-                '\core\services\request',
37
-                'EventEspresso\core\services\request'
38
-            ),
39
-            '4.9.52'
40
-        );
41
-        return $response;
42
-    }
20
+	/**
21
+	 * @deprecated
22
+	 * @param EE_Request  $request
23
+	 * @param EE_Response $response
24
+	 * @return EE_Response
25
+	 */
26
+	public function handle_request(EE_Request $request, EE_Response $response)
27
+	{
28
+		EE_Error::doing_it_wrong(
29
+			__METHOD__,
30
+			sprintf(
31
+				esc_html__(
32
+					'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
33
+					'event_espresso'
34
+				),
35
+				'EventEspresso\core\services\request\middleware\RecommendedVersions',
36
+				'\core\services\request',
37
+				'EventEspresso\core\services\request'
38
+			),
39
+			'4.9.52'
40
+		);
41
+		return $response;
42
+	}
43 43
 
44 44
 
45
-    /**
46
-     * @deprecated
47
-     * @param string $version_to_check
48
-     * @param string $operator
49
-     * @return bool
50
-     */
51
-    public static function check_wp_version($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
52
-    {
53
-        EE_Error::doing_it_wrong(
54
-            __METHOD__,
55
-            sprintf(
56
-                esc_html__(
57
-                    'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
58
-                    'event_espresso'
59
-                ),
60
-                'EventEspresso\core\services\request\middleware\RecommendedVersions::compareWordPressVersion()',
61
-                '\core\services\request',
62
-                'EventEspresso\core\services\request'
63
-            ),
64
-            '4.9.52',
65
-            '5.0.0'
66
-        );
67
-        return RecommendedVersions::compareWordPressVersion($version_to_check, $operator);
68
-    }
45
+	/**
46
+	 * @deprecated
47
+	 * @param string $version_to_check
48
+	 * @param string $operator
49
+	 * @return bool
50
+	 */
51
+	public static function check_wp_version($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
52
+	{
53
+		EE_Error::doing_it_wrong(
54
+			__METHOD__,
55
+			sprintf(
56
+				esc_html__(
57
+					'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
58
+					'event_espresso'
59
+				),
60
+				'EventEspresso\core\services\request\middleware\RecommendedVersions::compareWordPressVersion()',
61
+				'\core\services\request',
62
+				'EventEspresso\core\services\request'
63
+			),
64
+			'4.9.52',
65
+			'5.0.0'
66
+		);
67
+		return RecommendedVersions::compareWordPressVersion($version_to_check, $operator);
68
+	}
69 69
 
70 70
 
71
-    /**
72
-     * @deprecated
73
-     * @return void
74
-     */
75
-    public function minimum_wp_version_error()
76
-    {
77
-        EE_Error::doing_it_wrong(
78
-            __METHOD__,
79
-            sprintf(
80
-                esc_html__(
81
-                    'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
82
-                    'event_espresso'
83
-                ),
84
-                'EventEspresso\core\services\request\middleware\RecommendedVersions::minimumWpVersionError()',
85
-                '\core\services\request',
86
-                'EventEspresso\core\services\request'
87
-            ),
88
-            '4.9.52'
89
-        );
90
-    }
71
+	/**
72
+	 * @deprecated
73
+	 * @return void
74
+	 */
75
+	public function minimum_wp_version_error()
76
+	{
77
+		EE_Error::doing_it_wrong(
78
+			__METHOD__,
79
+			sprintf(
80
+				esc_html__(
81
+					'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
82
+					'event_espresso'
83
+				),
84
+				'EventEspresso\core\services\request\middleware\RecommendedVersions::minimumWpVersionError()',
85
+				'\core\services\request',
86
+				'EventEspresso\core\services\request'
87
+			),
88
+			'4.9.52'
89
+		);
90
+	}
91 91
 }
Please login to merge, or discard this patch.
core/middleware/EE_Middleware.core.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -20,27 +20,27 @@
 block discarded – undo
20 20
 abstract class EE_Middleware implements EEI_Request_Decorator
21 21
 {
22 22
 
23
-    /**
24
-     * @deprecated
25
-     * @param    EE_Request  $request
26
-     * @param    EE_Response $response
27
-     * @return    EE_Response
28
-     */
29
-    protected function process_request_stack(EE_Request $request, EE_Response $response)
30
-    {
31
-        EE_Error::doing_it_wrong(
32
-            __METHOD__,
33
-            sprintf(
34
-                esc_html__(
35
-                    'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
36
-                    'event_espresso'
37
-                ),
38
-                'EventEspresso\core\services\request\middleware\Middleware',
39
-                '\core\services\request',
40
-                'EventEspresso\core\services\request'
41
-            ),
42
-            '4.9.52'
43
-        );
44
-        return $response;
45
-    }
23
+	/**
24
+	 * @deprecated
25
+	 * @param    EE_Request  $request
26
+	 * @param    EE_Response $response
27
+	 * @return    EE_Response
28
+	 */
29
+	protected function process_request_stack(EE_Request $request, EE_Response $response)
30
+	{
31
+		EE_Error::doing_it_wrong(
32
+			__METHOD__,
33
+			sprintf(
34
+				esc_html__(
35
+					'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
36
+					'event_espresso'
37
+				),
38
+				'EventEspresso\core\services\request\middleware\Middleware',
39
+				'\core\services\request',
40
+				'EventEspresso\core\services\request'
41
+			),
42
+			'4.9.52'
43
+		);
44
+		return $response;
45
+	}
46 46
 }
Please login to merge, or discard this patch.
core/middleware/EE_Alpha_Banner_Warning.core.php 1 patch
Indentation   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -14,87 +14,87 @@
 block discarded – undo
14 14
 {
15 15
 
16 16
 
17
-    /**
18
-     * @deprecated 4.9.53
19
-     * @param    EE_Request  $request
20
-     * @param    EE_Response $response
21
-     * @return    EE_Response
22
-     */
23
-    public function handle_request(EE_Request $request, EE_Response $response)
24
-    {
25
-        $this->_request = $request;
26
-        $this->_response = $response;
27
-        $this->display_alpha_banner_warning();
28
-        $this->_response = $this->process_request_stack($this->_request, $this->_response);
29
-        return $this->_response;
30
-    }
17
+	/**
18
+	 * @deprecated 4.9.53
19
+	 * @param    EE_Request  $request
20
+	 * @param    EE_Response $response
21
+	 * @return    EE_Response
22
+	 */
23
+	public function handle_request(EE_Request $request, EE_Response $response)
24
+	{
25
+		$this->_request = $request;
26
+		$this->_response = $response;
27
+		$this->display_alpha_banner_warning();
28
+		$this->_response = $this->process_request_stack($this->_request, $this->_response);
29
+		return $this->_response;
30
+	}
31 31
 
32 32
 
33
-    /**
34
-     * @deprecated
35
-     * @return    string
36
-     */
37
-    public function display_alpha_banner_warning()
38
-    {
39
-        EE_Error::doing_it_wrong(
40
-            __METHOD__,
41
-            sprintf(
42
-                esc_html__(
43
-                    'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
44
-                    'event_espresso'
45
-                ),
46
-                'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::displayPreProductionVersionWarning()',
47
-                '\core\services\request',
48
-                'EventEspresso\core\services\request'
49
-            ),
50
-            '4.9.52',
51
-            '4.10.0'
52
-        );
53
-    }
33
+	/**
34
+	 * @deprecated
35
+	 * @return    string
36
+	 */
37
+	public function display_alpha_banner_warning()
38
+	{
39
+		EE_Error::doing_it_wrong(
40
+			__METHOD__,
41
+			sprintf(
42
+				esc_html__(
43
+					'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
44
+					'event_espresso'
45
+				),
46
+				'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::displayPreProductionVersionWarning()',
47
+				'\core\services\request',
48
+				'EventEspresso\core\services\request'
49
+			),
50
+			'4.9.52',
51
+			'4.10.0'
52
+		);
53
+	}
54 54
 
55 55
 
56
-    /**
57
-     * @deprecated
58
-     * @return void
59
-     */
60
-    public function alpha_banner_admin_notice()
61
-    {
62
-        EE_Error::doing_it_wrong(
63
-            __METHOD__,
64
-            sprintf(
65
-                esc_html__(
66
-                    'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
67
-                    'event_espresso'
68
-                ),
69
-                'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::preProductionVersionAdminNotice()',
70
-                '\core\services\request',
71
-                'EventEspresso\core\services\request'
72
-            ),
73
-            '4.9.52',
74
-            '4.10.0'
75
-        );
76
-    }
56
+	/**
57
+	 * @deprecated
58
+	 * @return void
59
+	 */
60
+	public function alpha_banner_admin_notice()
61
+	{
62
+		EE_Error::doing_it_wrong(
63
+			__METHOD__,
64
+			sprintf(
65
+				esc_html__(
66
+					'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
67
+					'event_espresso'
68
+				),
69
+				'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::preProductionVersionAdminNotice()',
70
+				'\core\services\request',
71
+				'EventEspresso\core\services\request'
72
+			),
73
+			'4.9.52',
74
+			'4.10.0'
75
+		);
76
+	}
77 77
 
78 78
 
79
-    /**
80
-     * @deprecated
81
-     * @return void
82
-     */
83
-    public function alpha_banner_warning_notice()
84
-    {
85
-        EE_Error::doing_it_wrong(
86
-            __METHOD__,
87
-            sprintf(
88
-                esc_html__(
89
-                    'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
90
-                    'event_espresso'
91
-                ),
92
-                'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::preProductionVersionWarningNotice()',
93
-                '\core\services\request',
94
-                'EventEspresso\core\services\request'
95
-            ),
96
-            '4.9.52',
97
-            '4.10.0'
98
-        );
99
-    }
79
+	/**
80
+	 * @deprecated
81
+	 * @return void
82
+	 */
83
+	public function alpha_banner_warning_notice()
84
+	{
85
+		EE_Error::doing_it_wrong(
86
+			__METHOD__,
87
+			sprintf(
88
+				esc_html__(
89
+					'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace',
90
+					'event_espresso'
91
+				),
92
+				'EventEspresso\core\services\request\middleware\PreProductionVersionWarning::preProductionVersionWarningNotice()',
93
+				'\core\services\request',
94
+				'EventEspresso\core\services\request'
95
+			),
96
+			'4.9.52',
97
+			'4.10.0'
98
+		);
99
+	}
100 100
 }
Please login to merge, or discard this patch.
core/EE_Encryption.core.php 2 patches
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
      */
104 104
     protected function __construct()
105 105
     {
106
-        if (! defined('ESPRESSO_ENCRYPT')) {
106
+        if ( ! defined('ESPRESSO_ENCRYPT')) {
107 107
             define('ESPRESSO_ENCRYPT', true);
108 108
         }
109 109
         if (extension_loaded('openssl')) {
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
     public static function instance()
126 126
     {
127 127
         // check if class object is instantiated
128
-        if (! EE_Encryption::$_instance instanceof EE_Encryption) {
128
+        if ( ! EE_Encryption::$_instance instanceof EE_Encryption) {
129 129
             EE_Encryption::$_instance = new self();
130 130
         }
131 131
         return EE_Encryption::$_instance;
@@ -332,7 +332,7 @@  discard block
 block discarded – undo
332 332
             $iv
333 333
         );
334 334
         // append the initialization vector
335
-        $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv;
335
+        $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER.$iv;
336 336
         // trim and maybe encode
337 337
         return $this->_use_base64_encode
338 338
             ? trim(base64_encode($encrypted_text))
@@ -525,8 +525,8 @@  discard block
 block discarded – undo
525 525
         );
526 526
         $string_bits = str_split($text_string);
527 527
         foreach ($string_bits as $k => $v) {
528
-            $temp = ord($v) + ord($key_bits[ $k ]);
529
-            $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp);
528
+            $temp = ord($v) + ord($key_bits[$k]);
529
+            $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp);
530 530
         }
531 531
         $encrypted_text = implode('', $string_bits);
532 532
         $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG;
@@ -570,8 +570,8 @@  discard block
 block discarded – undo
570 570
         );
571 571
         $string_bits = str_split($encrypted_text);
572 572
         foreach ($string_bits as $k => $v) {
573
-            $temp = ord($v) - ord($key_bits[ $k ]);
574
-            $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp);
573
+            $temp = ord($v) - ord($key_bits[$k]);
574
+            $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp);
575 575
         }
576 576
         return implode('', $string_bits);
577 577
     }
@@ -585,16 +585,16 @@  discard block
 block discarded – undo
585 585
     protected function valid_base_64($string)
586 586
     {
587 587
         // ensure data is a string
588
-        if (! is_string($string) || ! $this->_use_base64_encode) {
588
+        if ( ! is_string($string) || ! $this->_use_base64_encode) {
589 589
             return false;
590 590
         }
591 591
         $decoded = base64_decode($string, true);
592 592
         // Check if there is no invalid character in string
593
-        if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
593
+        if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
594 594
             return false;
595 595
         }
596 596
         // Decode the string in strict mode and send the response
597
-        if (! base64_decode($string, true)) {
597
+        if ( ! base64_decode($string, true)) {
598 598
             return false;
599 599
         }
600 600
         // Encode and compare it to original one
@@ -614,7 +614,7 @@  discard block
 block discarded – undo
614 614
         $iterations = ceil($length / 40);
615 615
         $random_string = '';
616 616
         for ($i = 0; $i < $iterations; $i++) {
617
-            $random_string .= sha1(microtime(true) . mt_rand(10000, 90000));
617
+            $random_string .= sha1(microtime(true).mt_rand(10000, 90000));
618 618
         }
619 619
         $random_string = substr($random_string, 0, $length);
620 620
         return $random_string;
Please login to merge, or discard this patch.
Indentation   +577 added lines, -577 removed lines patch added patch discarded remove patch
@@ -25,581 +25,581 @@
 block discarded – undo
25 25
 class EE_Encryption implements InterminableInterface
26 26
 {
27 27
 
28
-    /**
29
-     * key used for saving the encryption key to the wp_options table
30
-     */
31
-    const ENCRYPTION_OPTION_KEY = 'ee_encryption_key';
32
-
33
-    /**
34
-     * the OPENSSL cipher method used
35
-     */
36
-    const OPENSSL_CIPHER_METHOD = 'AES-128-CBC';
37
-
38
-    /**
39
-     * WP "options_name" used to store a verified available cipher method
40
-     */
41
-    const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method';
42
-
43
-    /**
44
-     * the OPENSSL digest method used
45
-     */
46
-    const OPENSSL_DIGEST_METHOD = 'sha512';
47
-
48
-    /**
49
-     * separates the encrypted text from the initialization vector
50
-     */
51
-    const OPENSSL_IV_DELIMITER = ':iv:';
52
-
53
-    /**
54
-     * appended to text encrypted using the acme encryption
55
-     */
56
-    const ACME_ENCRYPTION_FLAG = '::ae';
57
-
58
-
59
-    /**
60
-     * instance of the EE_Encryption object
61
-     */
62
-    protected static $_instance;
63
-
64
-    /**
65
-     * @var string $_encryption_key
66
-     */
67
-    protected $_encryption_key;
68
-
69
-    /**
70
-     * @var string $cipher_method
71
-     */
72
-    private $cipher_method = '';
73
-
74
-    /**
75
-     * @var array $cipher_methods
76
-     */
77
-    private $cipher_methods = array();
78
-
79
-    /**
80
-     * @var array $digest_methods
81
-     */
82
-    private $digest_methods = array();
83
-
84
-    /**
85
-     * @var boolean $_use_openssl_encrypt
86
-     */
87
-    protected $_use_openssl_encrypt = false;
88
-
89
-    /**
90
-     * @var boolean $_use_base64_encode
91
-     */
92
-    protected $_use_base64_encode = false;
93
-
94
-
95
-    /**
96
-     * protected constructor to prevent direct creation
97
-     */
98
-    protected function __construct()
99
-    {
100
-        if (! defined('ESPRESSO_ENCRYPT')) {
101
-            define('ESPRESSO_ENCRYPT', true);
102
-        }
103
-        if (extension_loaded('openssl')) {
104
-            $this->_use_openssl_encrypt = true;
105
-        }
106
-        if (function_exists('base64_encode')) {
107
-            $this->_use_base64_encode = true;
108
-        }
109
-    }
110
-
111
-
112
-    /**
113
-     * singleton method used to instantiate class object
114
-     *
115
-     * @return EE_Encryption
116
-     */
117
-    public static function instance()
118
-    {
119
-        // check if class object is instantiated
120
-        if (! EE_Encryption::$_instance instanceof EE_Encryption) {
121
-            EE_Encryption::$_instance = new self();
122
-        }
123
-        return EE_Encryption::$_instance;
124
-    }
125
-
126
-
127
-    /**
128
-     * get encryption key
129
-     *
130
-     * @return string
131
-     */
132
-    public function get_encryption_key()
133
-    {
134
-        // if encryption key has not been set
135
-        if (empty($this->_encryption_key)) {
136
-            // retrieve encryption_key from db
137
-            $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, '');
138
-            // WHAT?? No encryption_key in the db ??
139
-            if ($this->_encryption_key === '') {
140
-                // let's make one. And md5 it to make it just the right size for a key
141
-                $new_key = md5($this->generate_random_string());
142
-                // now save it to the db for later
143
-                add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key);
144
-                // here's the key - FINALLY !
145
-                $this->_encryption_key = $new_key;
146
-            }
147
-        }
148
-        return $this->_encryption_key;
149
-    }
150
-
151
-
152
-    /**
153
-     * encrypts data
154
-     *
155
-     * @param string $text_string - the text to be encrypted
156
-     * @return string
157
-     * @throws RuntimeException
158
-     */
159
-    public function encrypt($text_string = '')
160
-    {
161
-        // you give me nothing??? GET OUT !
162
-        if (empty($text_string)) {
163
-            return $text_string;
164
-        }
165
-        if ($this->_use_openssl_encrypt) {
166
-            $encrypted_text = $this->openssl_encrypt($text_string);
167
-        } else {
168
-            $encrypted_text = $this->acme_encrypt($text_string);
169
-        }
170
-        return $encrypted_text;
171
-    }
172
-
173
-
174
-    /**
175
-     * decrypts data
176
-     *
177
-     * @param string $encrypted_text - the text to be decrypted
178
-     * @return string
179
-     * @throws RuntimeException
180
-     */
181
-    public function decrypt($encrypted_text = '')
182
-    {
183
-        // you give me nothing??? GET OUT !
184
-        if (empty($encrypted_text)) {
185
-            return $encrypted_text;
186
-        }
187
-        // if PHP's mcrypt functions are installed then we'll use them
188
-        if ($this->_use_openssl_encrypt) {
189
-            $decrypted_text = $this->openssl_decrypt($encrypted_text);
190
-        } else {
191
-            $decrypted_text = $this->acme_decrypt($encrypted_text);
192
-        }
193
-        return $decrypted_text;
194
-    }
195
-
196
-
197
-    /**
198
-     * encodes string with PHP's base64 encoding
199
-     *
200
-     * @see http://php.net/manual/en/function.base64-encode.php
201
-     * @param string $text_string the text to be encoded
202
-     * @return string
203
-     */
204
-    public function base64_string_encode($text_string = '')
205
-    {
206
-        // you give me nothing??? GET OUT !
207
-        if (empty($text_string) || ! $this->_use_base64_encode) {
208
-            return $text_string;
209
-        }
210
-        // encode
211
-        return base64_encode($text_string);
212
-    }
213
-
214
-
215
-    /**
216
-     * decodes string that has been encoded with PHP's base64 encoding
217
-     *
218
-     * @see http://php.net/manual/en/function.base64-encode.php
219
-     * @param string $encoded_string the text to be decoded
220
-     * @return string
221
-     * @throws RuntimeException
222
-     */
223
-    public function base64_string_decode($encoded_string = '')
224
-    {
225
-        // you give me nothing??? GET OUT !
226
-        if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) {
227
-            return $encoded_string;
228
-        }
229
-        // decode
230
-        $decoded_string = base64_decode($encoded_string);
231
-        if ($decoded_string === false) {
232
-            throw new RuntimeException(
233
-                esc_html__('Base 64 decoding failed.', 'event_espresso')
234
-            );
235
-        }
236
-        return $decoded_string;
237
-    }
238
-
239
-
240
-    /**
241
-     * encodes  url string with PHP's base64 encoding
242
-     *
243
-     * @see http://php.net/manual/en/function.base64-encode.php
244
-     * @param string $text_string the text to be encoded
245
-     * @return string
246
-     */
247
-    public function base64_url_encode($text_string = '')
248
-    {
249
-        // you give me nothing??? GET OUT !
250
-        if (empty($text_string) || ! $this->_use_base64_encode) {
251
-            return $text_string;
252
-        }
253
-        // encode
254
-        $encoded_string = base64_encode($text_string);
255
-        // remove chars to make encoding more URL friendly
256
-        return strtr($encoded_string, '+/=', '-_,');
257
-    }
258
-
259
-
260
-    /**
261
-     * decodes  url string that has been encoded with PHP's base64 encoding
262
-     *
263
-     * @see http://php.net/manual/en/function.base64-encode.php
264
-     * @param string $encoded_string the text to be decoded
265
-     * @return string
266
-     * @throws RuntimeException
267
-     */
268
-    public function base64_url_decode($encoded_string = '')
269
-    {
270
-        // you give me nothing??? GET OUT !
271
-        if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) {
272
-            return $encoded_string;
273
-        }
274
-        // replace previously removed characters
275
-        $encoded_string = strtr($encoded_string, '-_,', '+/=');
276
-        // decode
277
-        $decoded_string = base64_decode($encoded_string);
278
-        if ($decoded_string === false) {
279
-            throw new RuntimeException(
280
-                esc_html__('Base 64 decoding failed.', 'event_espresso')
281
-            );
282
-        }
283
-        return $decoded_string;
284
-    }
285
-
286
-
287
-    /**
288
-     * encrypts data using PHP's openssl functions
289
-     *
290
-     * @param string $text_string the text to be encrypted
291
-     * @param string $cipher_method
292
-     * @param string $encryption_key
293
-     * @return string
294
-     * @throws RuntimeException
295
-     */
296
-    protected function openssl_encrypt(
297
-        $text_string = '',
298
-        $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD,
299
-        $encryption_key = ''
300
-    ) {
301
-        // you give me nothing??? GET OUT !
302
-        if (empty($text_string)) {
303
-            return $text_string;
304
-        }
305
-        $this->cipher_method = $this->getCipherMethod($cipher_method);
306
-        // get initialization vector size
307
-        $iv_size = openssl_cipher_iv_length($this->cipher_method);
308
-        // generate initialization vector.
309
-        // The second parameter ("crypto_strong") is passed by reference,
310
-        // and is used to determines if the algorithm used was "cryptographically strong"
311
-        // openssl_random_pseudo_bytes() will toggle it to either true or false
312
-        $iv = openssl_random_pseudo_bytes($iv_size, $is_strong);
313
-        if ($iv === false || $is_strong === false) {
314
-            throw new RuntimeException(
315
-                esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso')
316
-            );
317
-        }
318
-        // encrypt it
319
-        $encrypted_text = openssl_encrypt(
320
-            $text_string,
321
-            $this->cipher_method,
322
-            $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key),
323
-            0,
324
-            $iv
325
-        );
326
-        // append the initialization vector
327
-        $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv;
328
-        // trim and maybe encode
329
-        return $this->_use_base64_encode
330
-            ? trim(base64_encode($encrypted_text))
331
-            : trim($encrypted_text);
332
-    }
333
-
334
-
335
-    /**
336
-     * Returns a cipher method that has been verified to work.
337
-     * First checks if the cached cipher has been set already and if so, returns that.
338
-     * Then tests the incoming default and returns that if it's good.
339
-     * If not, then it retrieves the previously tested and saved cipher method.
340
-     * But if that doesn't exist, then calls getAvailableCipherMethod()
341
-     * to see what is available on the server, and returns the results.
342
-     *
343
-     * @param string $cipher_method
344
-     * @return string
345
-     * @throws RuntimeException
346
-     */
347
-    protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD)
348
-    {
349
-        if ($this->cipher_method !== '') {
350
-            return $this->cipher_method;
351
-        }
352
-        // verify that the default cipher method can produce an initialization vector
353
-        if (openssl_cipher_iv_length($cipher_method) === false) {
354
-            // nope? okay let's get what we found in the past to work
355
-            $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, '');
356
-            // oops... haven't tested available cipher methods yet
357
-            if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) {
358
-                $cipher_method = $this->getAvailableCipherMethod($cipher_method);
359
-            }
360
-        }
361
-        return $cipher_method;
362
-    }
363
-
364
-
365
-    /**
366
-     * @param string $cipher_method
367
-     * @return string
368
-     * @throws \RuntimeException
369
-     */
370
-    protected function getAvailableCipherMethod($cipher_method)
371
-    {
372
-        // verify that the incoming cipher method can produce an initialization vector
373
-        if (openssl_cipher_iv_length($cipher_method) === false) {
374
-            // nope? then check the next cipher in the list of available cipher methods
375
-            $cipher_method = next($this->cipher_methods);
376
-            // what? there's no list? then generate that list and cache it,
377
-            if (empty($this->cipher_methods)) {
378
-                $this->cipher_methods = openssl_get_cipher_methods();
379
-                // then grab the first item from the list
380
-                $cipher_method = reset($this->cipher_methods);
381
-            }
382
-            if ($cipher_method === false) {
383
-                throw new RuntimeException(
384
-                    esc_html__(
385
-                        'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.',
386
-                        'event_espresso'
387
-                    )
388
-                );
389
-            }
390
-            // verify that the next cipher method works
391
-            return $this->getAvailableCipherMethod($cipher_method);
392
-        }
393
-        // if we've gotten this far, then we found an available cipher method that works
394
-        // so save that for next time
395
-        update_option(
396
-            EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME,
397
-            $cipher_method
398
-        );
399
-        return $cipher_method;
400
-    }
401
-
402
-
403
-    /**
404
-     * decrypts data that has been encrypted with PHP's openssl functions
405
-     *
406
-     * @param string $encrypted_text the text to be decrypted
407
-     * @param string $cipher_method
408
-     * @param string $encryption_key
409
-     * @return string
410
-     * @throws RuntimeException
411
-     */
412
-    protected function openssl_decrypt(
413
-        $encrypted_text = '',
414
-        $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD,
415
-        $encryption_key = ''
416
-    ) {
417
-        // you give me nothing??? GET OUT !
418
-        if (empty($encrypted_text)) {
419
-            return $encrypted_text;
420
-        }
421
-        // decode
422
-        $encrypted_text = $this->valid_base_64($encrypted_text)
423
-            ? $this->base64_url_decode($encrypted_text)
424
-            : $encrypted_text;
425
-        $encrypted_components = explode(
426
-            EE_Encryption::OPENSSL_IV_DELIMITER,
427
-            $encrypted_text,
428
-            2
429
-        );
430
-        // decrypt it
431
-        $decrypted_text = openssl_decrypt(
432
-            $encrypted_components[0],
433
-            $this->getCipherMethod($cipher_method),
434
-            $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key),
435
-            0,
436
-            $encrypted_components[1]
437
-        );
438
-        $decrypted_text = trim($decrypted_text);
439
-        return $decrypted_text;
440
-    }
441
-
442
-
443
-    /**
444
-     * Computes the digest hash value using the specified digest method.
445
-     * If that digest method fails to produce a valid hash value,
446
-     * then we'll grab the next digest method and recursively try again until something works.
447
-     *
448
-     * @param string $digest_method
449
-     * @param string $encryption_key
450
-     * @return string
451
-     * @throws RuntimeException
452
-     */
453
-    protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '')
454
-    {
455
-        $encryption_key = $encryption_key !== ''
456
-            ? $encryption_key
457
-            : $this->get_encryption_key();
458
-        $digest_hash_value = openssl_digest($encryption_key, $digest_method);
459
-        if ($digest_hash_value === false) {
460
-            return $this->getDigestHashValue($this->getDigestMethod());
461
-        }
462
-        return $digest_hash_value;
463
-    }
464
-
465
-
466
-    /**
467
-     * Returns the NEXT element in the $digest_methods array.
468
-     * If the $digest_methods array is empty, then we populate it
469
-     * with the available values returned from openssl_get_md_methods().
470
-     *
471
-     * @return string
472
-     * @throws \RuntimeException
473
-     */
474
-    protected function getDigestMethod()
475
-    {
476
-        $digest_method = prev($this->digest_methods);
477
-        if (empty($this->digest_methods)) {
478
-            $this->digest_methods = openssl_get_md_methods();
479
-            $digest_method = end($this->digest_methods);
480
-        }
481
-        if ($digest_method === false) {
482
-            throw new RuntimeException(
483
-                esc_html__(
484
-                    'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.',
485
-                    'event_espresso'
486
-                )
487
-            );
488
-        }
489
-        return $digest_method;
490
-    }
491
-
492
-
493
-    /**
494
-     * encrypts data for acme servers that didn't bother to install PHP mcrypt
495
-     *
496
-     * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php
497
-     * @param string $text_string the text to be decrypted
498
-     * @return string
499
-     */
500
-    protected function acme_encrypt($text_string = '')
501
-    {
502
-        // you give me nothing??? GET OUT !
503
-        if (empty($text_string)) {
504
-            return $text_string;
505
-        }
506
-        $key_bits = str_split(
507
-            str_pad(
508
-                '',
509
-                strlen($text_string),
510
-                $this->get_encryption_key(),
511
-                STR_PAD_RIGHT
512
-            )
513
-        );
514
-        $string_bits = str_split($text_string);
515
-        foreach ($string_bits as $k => $v) {
516
-            $temp = ord($v) + ord($key_bits[ $k ]);
517
-            $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp);
518
-        }
519
-        $encrypted_text = implode('', $string_bits);
520
-        $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG;
521
-        return $this->_use_base64_encode
522
-            ? base64_encode($encrypted_text)
523
-            : $encrypted_text;
524
-    }
525
-
526
-
527
-    /**
528
-     * decrypts data for acme servers that didn't bother to install PHP mcrypt
529
-     *
530
-     * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php
531
-     * @param string $encrypted_text the text to be decrypted
532
-     * @return string
533
-     * @throws RuntimeException
534
-     */
535
-    protected function acme_decrypt($encrypted_text = '')
536
-    {
537
-        // you give me nothing??? GET OUT !
538
-        if (empty($encrypted_text)) {
539
-            return $encrypted_text;
540
-        }
541
-        // decode the data ?
542
-        $encrypted_text = $this->valid_base_64($encrypted_text)
543
-            ? $this->base64_url_decode($encrypted_text)
544
-            : $encrypted_text;
545
-        $encrypted_text = substr($encrypted_text, 0, -4);
546
-        $key_bits = str_split(
547
-            str_pad(
548
-                '',
549
-                strlen($encrypted_text),
550
-                $this->get_encryption_key(),
551
-                STR_PAD_RIGHT
552
-            )
553
-        );
554
-        $string_bits = str_split($encrypted_text);
555
-        foreach ($string_bits as $k => $v) {
556
-            $temp = ord($v) - ord($key_bits[ $k ]);
557
-            $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp);
558
-        }
559
-        return implode('', $string_bits);
560
-    }
561
-
562
-
563
-    /**
564
-     * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906
565
-     * @param $string
566
-     * @return bool
567
-     */
568
-    protected function valid_base_64($string)
569
-    {
570
-        // ensure data is a string
571
-        if (! is_string($string) || ! $this->_use_base64_encode) {
572
-            return false;
573
-        }
574
-        $decoded = base64_decode($string, true);
575
-        // Check if there is no invalid character in string
576
-        if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
577
-            return false;
578
-        }
579
-        // Decode the string in strict mode and send the response
580
-        if (! base64_decode($string, true)) {
581
-            return false;
582
-        }
583
-        // Encode and compare it to original one
584
-        return base64_encode($decoded) === $string;
585
-    }
586
-
587
-
588
-    /**
589
-     * generate random string
590
-     *
591
-     * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php
592
-     * @param int $length number of characters for random string
593
-     * @return string
594
-     */
595
-    public function generate_random_string($length = 40)
596
-    {
597
-        $iterations = ceil($length / 40);
598
-        $random_string = '';
599
-        for ($i = 0; $i < $iterations; $i++) {
600
-            $random_string .= sha1(microtime(true) . mt_rand(10000, 90000));
601
-        }
602
-        $random_string = substr($random_string, 0, $length);
603
-        return $random_string;
604
-    }
28
+	/**
29
+	 * key used for saving the encryption key to the wp_options table
30
+	 */
31
+	const ENCRYPTION_OPTION_KEY = 'ee_encryption_key';
32
+
33
+	/**
34
+	 * the OPENSSL cipher method used
35
+	 */
36
+	const OPENSSL_CIPHER_METHOD = 'AES-128-CBC';
37
+
38
+	/**
39
+	 * WP "options_name" used to store a verified available cipher method
40
+	 */
41
+	const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method';
42
+
43
+	/**
44
+	 * the OPENSSL digest method used
45
+	 */
46
+	const OPENSSL_DIGEST_METHOD = 'sha512';
47
+
48
+	/**
49
+	 * separates the encrypted text from the initialization vector
50
+	 */
51
+	const OPENSSL_IV_DELIMITER = ':iv:';
52
+
53
+	/**
54
+	 * appended to text encrypted using the acme encryption
55
+	 */
56
+	const ACME_ENCRYPTION_FLAG = '::ae';
57
+
58
+
59
+	/**
60
+	 * instance of the EE_Encryption object
61
+	 */
62
+	protected static $_instance;
63
+
64
+	/**
65
+	 * @var string $_encryption_key
66
+	 */
67
+	protected $_encryption_key;
68
+
69
+	/**
70
+	 * @var string $cipher_method
71
+	 */
72
+	private $cipher_method = '';
73
+
74
+	/**
75
+	 * @var array $cipher_methods
76
+	 */
77
+	private $cipher_methods = array();
78
+
79
+	/**
80
+	 * @var array $digest_methods
81
+	 */
82
+	private $digest_methods = array();
83
+
84
+	/**
85
+	 * @var boolean $_use_openssl_encrypt
86
+	 */
87
+	protected $_use_openssl_encrypt = false;
88
+
89
+	/**
90
+	 * @var boolean $_use_base64_encode
91
+	 */
92
+	protected $_use_base64_encode = false;
93
+
94
+
95
+	/**
96
+	 * protected constructor to prevent direct creation
97
+	 */
98
+	protected function __construct()
99
+	{
100
+		if (! defined('ESPRESSO_ENCRYPT')) {
101
+			define('ESPRESSO_ENCRYPT', true);
102
+		}
103
+		if (extension_loaded('openssl')) {
104
+			$this->_use_openssl_encrypt = true;
105
+		}
106
+		if (function_exists('base64_encode')) {
107
+			$this->_use_base64_encode = true;
108
+		}
109
+	}
110
+
111
+
112
+	/**
113
+	 * singleton method used to instantiate class object
114
+	 *
115
+	 * @return EE_Encryption
116
+	 */
117
+	public static function instance()
118
+	{
119
+		// check if class object is instantiated
120
+		if (! EE_Encryption::$_instance instanceof EE_Encryption) {
121
+			EE_Encryption::$_instance = new self();
122
+		}
123
+		return EE_Encryption::$_instance;
124
+	}
125
+
126
+
127
+	/**
128
+	 * get encryption key
129
+	 *
130
+	 * @return string
131
+	 */
132
+	public function get_encryption_key()
133
+	{
134
+		// if encryption key has not been set
135
+		if (empty($this->_encryption_key)) {
136
+			// retrieve encryption_key from db
137
+			$this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, '');
138
+			// WHAT?? No encryption_key in the db ??
139
+			if ($this->_encryption_key === '') {
140
+				// let's make one. And md5 it to make it just the right size for a key
141
+				$new_key = md5($this->generate_random_string());
142
+				// now save it to the db for later
143
+				add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key);
144
+				// here's the key - FINALLY !
145
+				$this->_encryption_key = $new_key;
146
+			}
147
+		}
148
+		return $this->_encryption_key;
149
+	}
150
+
151
+
152
+	/**
153
+	 * encrypts data
154
+	 *
155
+	 * @param string $text_string - the text to be encrypted
156
+	 * @return string
157
+	 * @throws RuntimeException
158
+	 */
159
+	public function encrypt($text_string = '')
160
+	{
161
+		// you give me nothing??? GET OUT !
162
+		if (empty($text_string)) {
163
+			return $text_string;
164
+		}
165
+		if ($this->_use_openssl_encrypt) {
166
+			$encrypted_text = $this->openssl_encrypt($text_string);
167
+		} else {
168
+			$encrypted_text = $this->acme_encrypt($text_string);
169
+		}
170
+		return $encrypted_text;
171
+	}
172
+
173
+
174
+	/**
175
+	 * decrypts data
176
+	 *
177
+	 * @param string $encrypted_text - the text to be decrypted
178
+	 * @return string
179
+	 * @throws RuntimeException
180
+	 */
181
+	public function decrypt($encrypted_text = '')
182
+	{
183
+		// you give me nothing??? GET OUT !
184
+		if (empty($encrypted_text)) {
185
+			return $encrypted_text;
186
+		}
187
+		// if PHP's mcrypt functions are installed then we'll use them
188
+		if ($this->_use_openssl_encrypt) {
189
+			$decrypted_text = $this->openssl_decrypt($encrypted_text);
190
+		} else {
191
+			$decrypted_text = $this->acme_decrypt($encrypted_text);
192
+		}
193
+		return $decrypted_text;
194
+	}
195
+
196
+
197
+	/**
198
+	 * encodes string with PHP's base64 encoding
199
+	 *
200
+	 * @see http://php.net/manual/en/function.base64-encode.php
201
+	 * @param string $text_string the text to be encoded
202
+	 * @return string
203
+	 */
204
+	public function base64_string_encode($text_string = '')
205
+	{
206
+		// you give me nothing??? GET OUT !
207
+		if (empty($text_string) || ! $this->_use_base64_encode) {
208
+			return $text_string;
209
+		}
210
+		// encode
211
+		return base64_encode($text_string);
212
+	}
213
+
214
+
215
+	/**
216
+	 * decodes string that has been encoded with PHP's base64 encoding
217
+	 *
218
+	 * @see http://php.net/manual/en/function.base64-encode.php
219
+	 * @param string $encoded_string the text to be decoded
220
+	 * @return string
221
+	 * @throws RuntimeException
222
+	 */
223
+	public function base64_string_decode($encoded_string = '')
224
+	{
225
+		// you give me nothing??? GET OUT !
226
+		if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) {
227
+			return $encoded_string;
228
+		}
229
+		// decode
230
+		$decoded_string = base64_decode($encoded_string);
231
+		if ($decoded_string === false) {
232
+			throw new RuntimeException(
233
+				esc_html__('Base 64 decoding failed.', 'event_espresso')
234
+			);
235
+		}
236
+		return $decoded_string;
237
+	}
238
+
239
+
240
+	/**
241
+	 * encodes  url string with PHP's base64 encoding
242
+	 *
243
+	 * @see http://php.net/manual/en/function.base64-encode.php
244
+	 * @param string $text_string the text to be encoded
245
+	 * @return string
246
+	 */
247
+	public function base64_url_encode($text_string = '')
248
+	{
249
+		// you give me nothing??? GET OUT !
250
+		if (empty($text_string) || ! $this->_use_base64_encode) {
251
+			return $text_string;
252
+		}
253
+		// encode
254
+		$encoded_string = base64_encode($text_string);
255
+		// remove chars to make encoding more URL friendly
256
+		return strtr($encoded_string, '+/=', '-_,');
257
+	}
258
+
259
+
260
+	/**
261
+	 * decodes  url string that has been encoded with PHP's base64 encoding
262
+	 *
263
+	 * @see http://php.net/manual/en/function.base64-encode.php
264
+	 * @param string $encoded_string the text to be decoded
265
+	 * @return string
266
+	 * @throws RuntimeException
267
+	 */
268
+	public function base64_url_decode($encoded_string = '')
269
+	{
270
+		// you give me nothing??? GET OUT !
271
+		if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) {
272
+			return $encoded_string;
273
+		}
274
+		// replace previously removed characters
275
+		$encoded_string = strtr($encoded_string, '-_,', '+/=');
276
+		// decode
277
+		$decoded_string = base64_decode($encoded_string);
278
+		if ($decoded_string === false) {
279
+			throw new RuntimeException(
280
+				esc_html__('Base 64 decoding failed.', 'event_espresso')
281
+			);
282
+		}
283
+		return $decoded_string;
284
+	}
285
+
286
+
287
+	/**
288
+	 * encrypts data using PHP's openssl functions
289
+	 *
290
+	 * @param string $text_string the text to be encrypted
291
+	 * @param string $cipher_method
292
+	 * @param string $encryption_key
293
+	 * @return string
294
+	 * @throws RuntimeException
295
+	 */
296
+	protected function openssl_encrypt(
297
+		$text_string = '',
298
+		$cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD,
299
+		$encryption_key = ''
300
+	) {
301
+		// you give me nothing??? GET OUT !
302
+		if (empty($text_string)) {
303
+			return $text_string;
304
+		}
305
+		$this->cipher_method = $this->getCipherMethod($cipher_method);
306
+		// get initialization vector size
307
+		$iv_size = openssl_cipher_iv_length($this->cipher_method);
308
+		// generate initialization vector.
309
+		// The second parameter ("crypto_strong") is passed by reference,
310
+		// and is used to determines if the algorithm used was "cryptographically strong"
311
+		// openssl_random_pseudo_bytes() will toggle it to either true or false
312
+		$iv = openssl_random_pseudo_bytes($iv_size, $is_strong);
313
+		if ($iv === false || $is_strong === false) {
314
+			throw new RuntimeException(
315
+				esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso')
316
+			);
317
+		}
318
+		// encrypt it
319
+		$encrypted_text = openssl_encrypt(
320
+			$text_string,
321
+			$this->cipher_method,
322
+			$this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key),
323
+			0,
324
+			$iv
325
+		);
326
+		// append the initialization vector
327
+		$encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv;
328
+		// trim and maybe encode
329
+		return $this->_use_base64_encode
330
+			? trim(base64_encode($encrypted_text))
331
+			: trim($encrypted_text);
332
+	}
333
+
334
+
335
+	/**
336
+	 * Returns a cipher method that has been verified to work.
337
+	 * First checks if the cached cipher has been set already and if so, returns that.
338
+	 * Then tests the incoming default and returns that if it's good.
339
+	 * If not, then it retrieves the previously tested and saved cipher method.
340
+	 * But if that doesn't exist, then calls getAvailableCipherMethod()
341
+	 * to see what is available on the server, and returns the results.
342
+	 *
343
+	 * @param string $cipher_method
344
+	 * @return string
345
+	 * @throws RuntimeException
346
+	 */
347
+	protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD)
348
+	{
349
+		if ($this->cipher_method !== '') {
350
+			return $this->cipher_method;
351
+		}
352
+		// verify that the default cipher method can produce an initialization vector
353
+		if (openssl_cipher_iv_length($cipher_method) === false) {
354
+			// nope? okay let's get what we found in the past to work
355
+			$cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, '');
356
+			// oops... haven't tested available cipher methods yet
357
+			if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) {
358
+				$cipher_method = $this->getAvailableCipherMethod($cipher_method);
359
+			}
360
+		}
361
+		return $cipher_method;
362
+	}
363
+
364
+
365
+	/**
366
+	 * @param string $cipher_method
367
+	 * @return string
368
+	 * @throws \RuntimeException
369
+	 */
370
+	protected function getAvailableCipherMethod($cipher_method)
371
+	{
372
+		// verify that the incoming cipher method can produce an initialization vector
373
+		if (openssl_cipher_iv_length($cipher_method) === false) {
374
+			// nope? then check the next cipher in the list of available cipher methods
375
+			$cipher_method = next($this->cipher_methods);
376
+			// what? there's no list? then generate that list and cache it,
377
+			if (empty($this->cipher_methods)) {
378
+				$this->cipher_methods = openssl_get_cipher_methods();
379
+				// then grab the first item from the list
380
+				$cipher_method = reset($this->cipher_methods);
381
+			}
382
+			if ($cipher_method === false) {
383
+				throw new RuntimeException(
384
+					esc_html__(
385
+						'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.',
386
+						'event_espresso'
387
+					)
388
+				);
389
+			}
390
+			// verify that the next cipher method works
391
+			return $this->getAvailableCipherMethod($cipher_method);
392
+		}
393
+		// if we've gotten this far, then we found an available cipher method that works
394
+		// so save that for next time
395
+		update_option(
396
+			EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME,
397
+			$cipher_method
398
+		);
399
+		return $cipher_method;
400
+	}
401
+
402
+
403
+	/**
404
+	 * decrypts data that has been encrypted with PHP's openssl functions
405
+	 *
406
+	 * @param string $encrypted_text the text to be decrypted
407
+	 * @param string $cipher_method
408
+	 * @param string $encryption_key
409
+	 * @return string
410
+	 * @throws RuntimeException
411
+	 */
412
+	protected function openssl_decrypt(
413
+		$encrypted_text = '',
414
+		$cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD,
415
+		$encryption_key = ''
416
+	) {
417
+		// you give me nothing??? GET OUT !
418
+		if (empty($encrypted_text)) {
419
+			return $encrypted_text;
420
+		}
421
+		// decode
422
+		$encrypted_text = $this->valid_base_64($encrypted_text)
423
+			? $this->base64_url_decode($encrypted_text)
424
+			: $encrypted_text;
425
+		$encrypted_components = explode(
426
+			EE_Encryption::OPENSSL_IV_DELIMITER,
427
+			$encrypted_text,
428
+			2
429
+		);
430
+		// decrypt it
431
+		$decrypted_text = openssl_decrypt(
432
+			$encrypted_components[0],
433
+			$this->getCipherMethod($cipher_method),
434
+			$this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key),
435
+			0,
436
+			$encrypted_components[1]
437
+		);
438
+		$decrypted_text = trim($decrypted_text);
439
+		return $decrypted_text;
440
+	}
441
+
442
+
443
+	/**
444
+	 * Computes the digest hash value using the specified digest method.
445
+	 * If that digest method fails to produce a valid hash value,
446
+	 * then we'll grab the next digest method and recursively try again until something works.
447
+	 *
448
+	 * @param string $digest_method
449
+	 * @param string $encryption_key
450
+	 * @return string
451
+	 * @throws RuntimeException
452
+	 */
453
+	protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '')
454
+	{
455
+		$encryption_key = $encryption_key !== ''
456
+			? $encryption_key
457
+			: $this->get_encryption_key();
458
+		$digest_hash_value = openssl_digest($encryption_key, $digest_method);
459
+		if ($digest_hash_value === false) {
460
+			return $this->getDigestHashValue($this->getDigestMethod());
461
+		}
462
+		return $digest_hash_value;
463
+	}
464
+
465
+
466
+	/**
467
+	 * Returns the NEXT element in the $digest_methods array.
468
+	 * If the $digest_methods array is empty, then we populate it
469
+	 * with the available values returned from openssl_get_md_methods().
470
+	 *
471
+	 * @return string
472
+	 * @throws \RuntimeException
473
+	 */
474
+	protected function getDigestMethod()
475
+	{
476
+		$digest_method = prev($this->digest_methods);
477
+		if (empty($this->digest_methods)) {
478
+			$this->digest_methods = openssl_get_md_methods();
479
+			$digest_method = end($this->digest_methods);
480
+		}
481
+		if ($digest_method === false) {
482
+			throw new RuntimeException(
483
+				esc_html__(
484
+					'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.',
485
+					'event_espresso'
486
+				)
487
+			);
488
+		}
489
+		return $digest_method;
490
+	}
491
+
492
+
493
+	/**
494
+	 * encrypts data for acme servers that didn't bother to install PHP mcrypt
495
+	 *
496
+	 * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php
497
+	 * @param string $text_string the text to be decrypted
498
+	 * @return string
499
+	 */
500
+	protected function acme_encrypt($text_string = '')
501
+	{
502
+		// you give me nothing??? GET OUT !
503
+		if (empty($text_string)) {
504
+			return $text_string;
505
+		}
506
+		$key_bits = str_split(
507
+			str_pad(
508
+				'',
509
+				strlen($text_string),
510
+				$this->get_encryption_key(),
511
+				STR_PAD_RIGHT
512
+			)
513
+		);
514
+		$string_bits = str_split($text_string);
515
+		foreach ($string_bits as $k => $v) {
516
+			$temp = ord($v) + ord($key_bits[ $k ]);
517
+			$string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp);
518
+		}
519
+		$encrypted_text = implode('', $string_bits);
520
+		$encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG;
521
+		return $this->_use_base64_encode
522
+			? base64_encode($encrypted_text)
523
+			: $encrypted_text;
524
+	}
525
+
526
+
527
+	/**
528
+	 * decrypts data for acme servers that didn't bother to install PHP mcrypt
529
+	 *
530
+	 * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php
531
+	 * @param string $encrypted_text the text to be decrypted
532
+	 * @return string
533
+	 * @throws RuntimeException
534
+	 */
535
+	protected function acme_decrypt($encrypted_text = '')
536
+	{
537
+		// you give me nothing??? GET OUT !
538
+		if (empty($encrypted_text)) {
539
+			return $encrypted_text;
540
+		}
541
+		// decode the data ?
542
+		$encrypted_text = $this->valid_base_64($encrypted_text)
543
+			? $this->base64_url_decode($encrypted_text)
544
+			: $encrypted_text;
545
+		$encrypted_text = substr($encrypted_text, 0, -4);
546
+		$key_bits = str_split(
547
+			str_pad(
548
+				'',
549
+				strlen($encrypted_text),
550
+				$this->get_encryption_key(),
551
+				STR_PAD_RIGHT
552
+			)
553
+		);
554
+		$string_bits = str_split($encrypted_text);
555
+		foreach ($string_bits as $k => $v) {
556
+			$temp = ord($v) - ord($key_bits[ $k ]);
557
+			$string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp);
558
+		}
559
+		return implode('', $string_bits);
560
+	}
561
+
562
+
563
+	/**
564
+	 * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906
565
+	 * @param $string
566
+	 * @return bool
567
+	 */
568
+	protected function valid_base_64($string)
569
+	{
570
+		// ensure data is a string
571
+		if (! is_string($string) || ! $this->_use_base64_encode) {
572
+			return false;
573
+		}
574
+		$decoded = base64_decode($string, true);
575
+		// Check if there is no invalid character in string
576
+		if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
577
+			return false;
578
+		}
579
+		// Decode the string in strict mode and send the response
580
+		if (! base64_decode($string, true)) {
581
+			return false;
582
+		}
583
+		// Encode and compare it to original one
584
+		return base64_encode($decoded) === $string;
585
+	}
586
+
587
+
588
+	/**
589
+	 * generate random string
590
+	 *
591
+	 * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php
592
+	 * @param int $length number of characters for random string
593
+	 * @return string
594
+	 */
595
+	public function generate_random_string($length = 40)
596
+	{
597
+		$iterations = ceil($length / 40);
598
+		$random_string = '';
599
+		for ($i = 0; $i < $iterations; $i++) {
600
+			$random_string .= sha1(microtime(true) . mt_rand(10000, 90000));
601
+		}
602
+		$random_string = substr($random_string, 0, $length);
603
+		return $random_string;
604
+	}
605 605
 }
Please login to merge, or discard this patch.
core/EE_Module_Request_Router.core.php 2 patches
Indentation   +235 added lines, -235 removed lines patch added patch discarded remove patch
@@ -15,255 +15,255 @@
 block discarded – undo
15 15
 final class EE_Module_Request_Router implements InterminableInterface
16 16
 {
17 17
 
18
-    /**
19
-     * @var EE_Request $request
20
-     */
21
-    private $request;
18
+	/**
19
+	 * @var EE_Request $request
20
+	 */
21
+	private $request;
22 22
 
23
-    /**
24
-     * @var array $_previous_routes
25
-     */
26
-    private static $_previous_routes = array();
23
+	/**
24
+	 * @var array $_previous_routes
25
+	 */
26
+	private static $_previous_routes = array();
27 27
 
28
-    /**
29
-     * @var WP_Query $WP_Query
30
-     */
31
-    public $WP_Query;
28
+	/**
29
+	 * @var WP_Query $WP_Query
30
+	 */
31
+	public $WP_Query;
32 32
 
33 33
 
34
-    /**
35
-     * EE_Module_Request_Router constructor.
36
-     *
37
-     * @param EE_Request $request
38
-     */
39
-    public function __construct(EE_Request $request)
40
-    {
41
-        $this->request = $request;
42
-    }
34
+	/**
35
+	 * EE_Module_Request_Router constructor.
36
+	 *
37
+	 * @param EE_Request $request
38
+	 */
39
+	public function __construct(EE_Request $request)
40
+	{
41
+		$this->request = $request;
42
+	}
43 43
 
44 44
 
45
-    /**
46
-     * on the first call  to this method, it checks the EE_Request_Handler for a "route"
47
-     * on subsequent calls to this method,
48
-     * instead of checking the EE_Request_Handler for a route, it checks the previous routes array,
49
-     * and checks if the last called route has any forwarding routes registered for it
50
-     *
51
-     * @param WP_Query $WP_Query
52
-     * @return NULL|string
53
-     * @throws EE_Error
54
-     * @throws ReflectionException
55
-     */
56
-    public function get_route(WP_Query $WP_Query)
57
-    {
58
-        $this->WP_Query = $WP_Query;
59
-        // assume this if first route being called
60
-        $previous_route = false;
61
-        // but is it really ???
62
-        if (! empty(self::$_previous_routes)) {
63
-            // get last run route
64
-            $previous_routes = array_values(self::$_previous_routes);
65
-            $previous_route = array_pop($previous_routes);
66
-        }
67
-        //  has another route already been run ?
68
-        if ($previous_route) {
69
-            // check if  forwarding has been set
70
-            $current_route = $this->get_forward($previous_route);
71
-            try {
72
-                // check for recursive forwarding
73
-                if (isset(self::$_previous_routes[ $current_route ])) {
74
-                    throw new EE_Error(
75
-                        sprintf(
76
-                            __(
77
-                                'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.',
78
-                                'event_espresso'
79
-                            ),
80
-                            $current_route
81
-                        )
82
-                    );
83
-                }
84
-            } catch (EE_Error $e) {
85
-                $e->get_error();
86
-                return null;
87
-            }
88
-        } else {
89
-            // first route called
90
-            $current_route = null;
91
-            // grab all routes
92
-            $routes = EE_Config::get_routes();
93
-            foreach ($routes as $key => $route) {
94
-                // first determine if route key uses w?ldc*rds
95
-                $uses_wildcards = strpos($key, '?') !== false
96
-                                  || strpos($key, '*') !== false;
97
-                // check request for module route
98
-                $route_found = $uses_wildcards
99
-                    ? $this->request->matches($key)
100
-                    : $this->request->is_set($key);
101
-                if ($route_found) {
102
-                    $current_route = $uses_wildcards
103
-                        ? $this->request->getMatch($key)
104
-                        : $this->request->get($key);
105
-                    $current_route = sanitize_text_field($current_route);
106
-                    if ($current_route) {
107
-                        $current_route = array($key, $current_route);
108
-                        break;
109
-                    }
110
-                }
111
-            }
112
-        }
113
-        // sorry, but I can't read what you route !
114
-        if (empty($current_route)) {
115
-            return null;
116
-        }
117
-        // add route to previous routes array
118
-        self::$_previous_routes[] = $current_route;
119
-        return $current_route;
120
-    }
45
+	/**
46
+	 * on the first call  to this method, it checks the EE_Request_Handler for a "route"
47
+	 * on subsequent calls to this method,
48
+	 * instead of checking the EE_Request_Handler for a route, it checks the previous routes array,
49
+	 * and checks if the last called route has any forwarding routes registered for it
50
+	 *
51
+	 * @param WP_Query $WP_Query
52
+	 * @return NULL|string
53
+	 * @throws EE_Error
54
+	 * @throws ReflectionException
55
+	 */
56
+	public function get_route(WP_Query $WP_Query)
57
+	{
58
+		$this->WP_Query = $WP_Query;
59
+		// assume this if first route being called
60
+		$previous_route = false;
61
+		// but is it really ???
62
+		if (! empty(self::$_previous_routes)) {
63
+			// get last run route
64
+			$previous_routes = array_values(self::$_previous_routes);
65
+			$previous_route = array_pop($previous_routes);
66
+		}
67
+		//  has another route already been run ?
68
+		if ($previous_route) {
69
+			// check if  forwarding has been set
70
+			$current_route = $this->get_forward($previous_route);
71
+			try {
72
+				// check for recursive forwarding
73
+				if (isset(self::$_previous_routes[ $current_route ])) {
74
+					throw new EE_Error(
75
+						sprintf(
76
+							__(
77
+								'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.',
78
+								'event_espresso'
79
+							),
80
+							$current_route
81
+						)
82
+					);
83
+				}
84
+			} catch (EE_Error $e) {
85
+				$e->get_error();
86
+				return null;
87
+			}
88
+		} else {
89
+			// first route called
90
+			$current_route = null;
91
+			// grab all routes
92
+			$routes = EE_Config::get_routes();
93
+			foreach ($routes as $key => $route) {
94
+				// first determine if route key uses w?ldc*rds
95
+				$uses_wildcards = strpos($key, '?') !== false
96
+								  || strpos($key, '*') !== false;
97
+				// check request for module route
98
+				$route_found = $uses_wildcards
99
+					? $this->request->matches($key)
100
+					: $this->request->is_set($key);
101
+				if ($route_found) {
102
+					$current_route = $uses_wildcards
103
+						? $this->request->getMatch($key)
104
+						: $this->request->get($key);
105
+					$current_route = sanitize_text_field($current_route);
106
+					if ($current_route) {
107
+						$current_route = array($key, $current_route);
108
+						break;
109
+					}
110
+				}
111
+			}
112
+		}
113
+		// sorry, but I can't read what you route !
114
+		if (empty($current_route)) {
115
+			return null;
116
+		}
117
+		// add route to previous routes array
118
+		self::$_previous_routes[] = $current_route;
119
+		return $current_route;
120
+	}
121 121
 
122 122
 
123
-    /**
124
-     * this method simply takes a valid route, and resolves what module class method the route points to
125
-     *
126
-     * @param string $key
127
-     * @param string $current_route
128
-     * @return mixed EED_Module | boolean
129
-     * @throws EE_Error
130
-     * @throws ReflectionException
131
-     */
132
-    public function resolve_route($key, $current_route)
133
-    {
134
-        // get module method that route has been mapped to
135
-        $module_method = EE_Config::get_route($current_route, $key);
136
-        // verify result was returned
137
-        if (empty($module_method)) {
138
-            $msg = sprintf(
139
-                __('The requested route %s could not be mapped to any registered modules.', 'event_espresso'),
140
-                $current_route
141
-            );
142
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
143
-            return false;
144
-        }
145
-        // verify that result is an array
146
-        if (! is_array($module_method)) {
147
-            $msg = sprintf(__('The %s  route has not been properly registered.', 'event_espresso'), $current_route);
148
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
149
-            return false;
150
-        }
151
-        // grab module name
152
-        $module_name = $module_method[0];
153
-        // verify that a class method was registered properly
154
-        if (! isset($module_method[1])) {
155
-            $msg = sprintf(
156
-                __('A class method for the %s  route has not been properly registered.', 'event_espresso'),
157
-                $current_route
158
-            );
159
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
160
-            return false;
161
-        }
162
-        // grab method
163
-        $method = $module_method[1];
164
-        // verify that class exists
165
-        if (! class_exists($module_name)) {
166
-            $msg = sprintf(__('The requested %s class could not be found.', 'event_espresso'), $module_name);
167
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
168
-            return false;
169
-        }
170
-        // verify that method exists
171
-        if (! method_exists($module_name, $method)) {
172
-            $msg = sprintf(
173
-                __('The class method %s for the %s route is in invalid.', 'event_espresso'),
174
-                $method,
175
-                $current_route
176
-            );
177
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
178
-            return false;
179
-        }
180
-        // instantiate module and call route method
181
-        return $this->_module_router($module_name, $method);
182
-    }
123
+	/**
124
+	 * this method simply takes a valid route, and resolves what module class method the route points to
125
+	 *
126
+	 * @param string $key
127
+	 * @param string $current_route
128
+	 * @return mixed EED_Module | boolean
129
+	 * @throws EE_Error
130
+	 * @throws ReflectionException
131
+	 */
132
+	public function resolve_route($key, $current_route)
133
+	{
134
+		// get module method that route has been mapped to
135
+		$module_method = EE_Config::get_route($current_route, $key);
136
+		// verify result was returned
137
+		if (empty($module_method)) {
138
+			$msg = sprintf(
139
+				__('The requested route %s could not be mapped to any registered modules.', 'event_espresso'),
140
+				$current_route
141
+			);
142
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
143
+			return false;
144
+		}
145
+		// verify that result is an array
146
+		if (! is_array($module_method)) {
147
+			$msg = sprintf(__('The %s  route has not been properly registered.', 'event_espresso'), $current_route);
148
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
149
+			return false;
150
+		}
151
+		// grab module name
152
+		$module_name = $module_method[0];
153
+		// verify that a class method was registered properly
154
+		if (! isset($module_method[1])) {
155
+			$msg = sprintf(
156
+				__('A class method for the %s  route has not been properly registered.', 'event_espresso'),
157
+				$current_route
158
+			);
159
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
160
+			return false;
161
+		}
162
+		// grab method
163
+		$method = $module_method[1];
164
+		// verify that class exists
165
+		if (! class_exists($module_name)) {
166
+			$msg = sprintf(__('The requested %s class could not be found.', 'event_espresso'), $module_name);
167
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
168
+			return false;
169
+		}
170
+		// verify that method exists
171
+		if (! method_exists($module_name, $method)) {
172
+			$msg = sprintf(
173
+				__('The class method %s for the %s route is in invalid.', 'event_espresso'),
174
+				$method,
175
+				$current_route
176
+			);
177
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
178
+			return false;
179
+		}
180
+		// instantiate module and call route method
181
+		return $this->_module_router($module_name, $method);
182
+	}
183 183
 
184 184
 
185
-    /**
186
-     * this method instantiates modules and calls the method that was defined when the route was registered
187
-     *
188
-     * @param string $module_name
189
-     * @return EED_Module|object|null
190
-     * @throws ReflectionException
191
-     */
192
-    public static function module_factory($module_name)
193
-    {
194
-        if ($module_name === 'EED_Module') {
195
-            EE_Error::add_error(
196
-                sprintf(
197
-                    __(
198
-                        'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.',
199
-                        'event_espresso'
200
-                    ),
201
-                    $module_name
202
-                ),
203
-                __FILE__,
204
-                __FUNCTION__,
205
-                __LINE__
206
-            );
207
-            return null;
208
-        }
209
-        // instantiate module class
210
-        $module = new $module_name();
211
-        // ensure that class is actually a module
212
-        if (! $module instanceof EED_Module) {
213
-            EE_Error::add_error(
214
-                sprintf(__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name),
215
-                __FILE__,
216
-                __FUNCTION__,
217
-                __LINE__
218
-            );
219
-            return null;
220
-        }
221
-        return $module;
222
-    }
185
+	/**
186
+	 * this method instantiates modules and calls the method that was defined when the route was registered
187
+	 *
188
+	 * @param string $module_name
189
+	 * @return EED_Module|object|null
190
+	 * @throws ReflectionException
191
+	 */
192
+	public static function module_factory($module_name)
193
+	{
194
+		if ($module_name === 'EED_Module') {
195
+			EE_Error::add_error(
196
+				sprintf(
197
+					__(
198
+						'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.',
199
+						'event_espresso'
200
+					),
201
+					$module_name
202
+				),
203
+				__FILE__,
204
+				__FUNCTION__,
205
+				__LINE__
206
+			);
207
+			return null;
208
+		}
209
+		// instantiate module class
210
+		$module = new $module_name();
211
+		// ensure that class is actually a module
212
+		if (! $module instanceof EED_Module) {
213
+			EE_Error::add_error(
214
+				sprintf(__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name),
215
+				__FILE__,
216
+				__FUNCTION__,
217
+				__LINE__
218
+			);
219
+			return null;
220
+		}
221
+		return $module;
222
+	}
223 223
 
224 224
 
225
-    /**
226
-     * this method instantiates modules and calls the method that was defined when the route was registered
227
-     *
228
-     * @param string $module_name
229
-     * @param string $method
230
-     * @return EED_Module|null
231
-     * @throws EE_Error
232
-     * @throws ReflectionException
233
-     */
234
-    private function _module_router($module_name, $method)
235
-    {
236
-        // instantiate module class
237
-        $module = EE_Module_Request_Router::module_factory($module_name);
238
-        if ($module instanceof EED_Module) {
239
-            // and call whatever action the route was for
240
-            try {
241
-                call_user_func(array($module, $method), $this->WP_Query);
242
-            } catch (EE_Error $e) {
243
-                $e->get_error();
244
-                return null;
245
-            }
246
-        }
247
-        return $module;
248
-    }
225
+	/**
226
+	 * this method instantiates modules and calls the method that was defined when the route was registered
227
+	 *
228
+	 * @param string $module_name
229
+	 * @param string $method
230
+	 * @return EED_Module|null
231
+	 * @throws EE_Error
232
+	 * @throws ReflectionException
233
+	 */
234
+	private function _module_router($module_name, $method)
235
+	{
236
+		// instantiate module class
237
+		$module = EE_Module_Request_Router::module_factory($module_name);
238
+		if ($module instanceof EED_Module) {
239
+			// and call whatever action the route was for
240
+			try {
241
+				call_user_func(array($module, $method), $this->WP_Query);
242
+			} catch (EE_Error $e) {
243
+				$e->get_error();
244
+				return null;
245
+			}
246
+		}
247
+		return $module;
248
+	}
249 249
 
250 250
 
251
-    /**
252
-     * @param $current_route
253
-     * @return string
254
-     */
255
-    public function get_forward($current_route)
256
-    {
257
-        return EE_Config::get_forward($current_route);
258
-    }
251
+	/**
252
+	 * @param $current_route
253
+	 * @return string
254
+	 */
255
+	public function get_forward($current_route)
256
+	{
257
+		return EE_Config::get_forward($current_route);
258
+	}
259 259
 
260 260
 
261
-    /**
262
-     * @param $current_route
263
-     * @return string
264
-     */
265
-    public function get_view($current_route)
266
-    {
267
-        return EE_Config::get_view($current_route);
268
-    }
261
+	/**
262
+	 * @param $current_route
263
+	 * @return string
264
+	 */
265
+	public function get_view($current_route)
266
+	{
267
+		return EE_Config::get_view($current_route);
268
+	}
269 269
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
         // assume this if first route being called
60 60
         $previous_route = false;
61 61
         // but is it really ???
62
-        if (! empty(self::$_previous_routes)) {
62
+        if ( ! empty(self::$_previous_routes)) {
63 63
             // get last run route
64 64
             $previous_routes = array_values(self::$_previous_routes);
65 65
             $previous_route = array_pop($previous_routes);
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
             $current_route = $this->get_forward($previous_route);
71 71
             try {
72 72
                 // check for recursive forwarding
73
-                if (isset(self::$_previous_routes[ $current_route ])) {
73
+                if (isset(self::$_previous_routes[$current_route])) {
74 74
                     throw new EE_Error(
75 75
                         sprintf(
76 76
                             __(
@@ -143,38 +143,38 @@  discard block
 block discarded – undo
143 143
             return false;
144 144
         }
145 145
         // verify that result is an array
146
-        if (! is_array($module_method)) {
146
+        if ( ! is_array($module_method)) {
147 147
             $msg = sprintf(__('The %s  route has not been properly registered.', 'event_espresso'), $current_route);
148
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
148
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
149 149
             return false;
150 150
         }
151 151
         // grab module name
152 152
         $module_name = $module_method[0];
153 153
         // verify that a class method was registered properly
154
-        if (! isset($module_method[1])) {
154
+        if ( ! isset($module_method[1])) {
155 155
             $msg = sprintf(
156 156
                 __('A class method for the %s  route has not been properly registered.', 'event_espresso'),
157 157
                 $current_route
158 158
             );
159
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
159
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
160 160
             return false;
161 161
         }
162 162
         // grab method
163 163
         $method = $module_method[1];
164 164
         // verify that class exists
165
-        if (! class_exists($module_name)) {
165
+        if ( ! class_exists($module_name)) {
166 166
             $msg = sprintf(__('The requested %s class could not be found.', 'event_espresso'), $module_name);
167 167
             EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
168 168
             return false;
169 169
         }
170 170
         // verify that method exists
171
-        if (! method_exists($module_name, $method)) {
171
+        if ( ! method_exists($module_name, $method)) {
172 172
             $msg = sprintf(
173 173
                 __('The class method %s for the %s route is in invalid.', 'event_espresso'),
174 174
                 $method,
175 175
                 $current_route
176 176
             );
177
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
177
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
178 178
             return false;
179 179
         }
180 180
         // instantiate module and call route method
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
         // instantiate module class
210 210
         $module = new $module_name();
211 211
         // ensure that class is actually a module
212
-        if (! $module instanceof EED_Module) {
212
+        if ( ! $module instanceof EED_Module) {
213 213
             EE_Error::add_error(
214 214
                 sprintf(__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name),
215 215
                 __FILE__,
Please login to merge, or discard this patch.
core/templates/espresso-ajax-notices.template.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,9 +1,9 @@
 block discarded – undo
1 1
 <div id="espresso-ajax-loading" style="display:none;">
2 2
     <span class="ee-spinner ee-spin"></span><span style="display:none;">
3 3
         <?php _e(
4
-            'loading...',
5
-            'event_espresso'
6
-        ); ?></span>
4
+			'loading...',
5
+			'event_espresso'
6
+		); ?></span>
7 7
 </div>
8 8
 
9 9
 <div id="espresso-ajax-notices">
Please login to merge, or discard this patch.