-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.mobicart.php
167 lines (136 loc) · 4.82 KB
/
class.mobicart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
require_once("class.request.php");
require_once("class.mobicart.store.php");
require_once("class.mobicart.product.php");
require_once("class.mobicart.user.php");
require_once("class.mobicart.images.php");
require_once("class.mobicart.order.php");
require_once("class.mobicart.department.php");
class Mobicart extends Request {
var $base_url = "www.mobi-cart.com/api";
var $aliases = array();
function Mobicart($key) {
parent::Request($this->base_url);
$this->set_key($key);
$this->store = new Mobicart_Store($this);
$this->product = new Mobicart_Product($this);
$this->user = new Mobicart_User($this);
$this->images = new Mobicart_Images($this);
$this->order = new Mobicart_Order($this);
$this->department = new Mobicart_Department($this);
$this->dept =& $this->department;
}
function set_key($key) {
$this->remove("api_key");
$this->key = $key;
$this->add_param("api_key", $key, true);
return $this;
}
/**
* Add a function alias for non-ambiguous call().
*/
function add_alias($alias, $callback) {
$this->aliases[$alias] = $callback;
}
function __call($name, $arguments) {
if(isset($this->aliases[$name]) && is_callable($this->aliases[$name]))
return call_user_func_array($this->aliases[$name], $arguments);
return call_user_func_array(array($this,$name), $arguments);
}
/**
* The fields the next request should require/validate
*/
function requires() {
$this->requires = func_get_args();
return $this;
}
/**
* The fields the next request should include but not require
*/
function optional() {
$this->optional = func_get_args();
return $this;
}
/**
* Checks all fields in @requires for existence and correctness
*/
function validate() {
$this->validation_error = false;
if(!$this->has("url", "method")) {
$this->validation_error = "No method set";
return false;
}
$method = $this->get("url", "method");
foreach($this->requires as $key) {
$value = $this->get("param", $key);
if($value === FALSE)
$this->validation_error = "Method $method requires a $key.";
else
$this->check($key, $value);
}
return !($this->validation_error);
}
function check($key, $value) {
switch ($key) {
case "user_name": #global
case "s_merchant_paypal_email": # order add
case "s_buyer_email": # order add
case "payPalAddress":
if(!strpos($value, "@"))
$this->validation_error = "User_name must be an email address.";
break;
case "from_date": # order get_by_date
case "to_date": # order get_by_date
case "d_order_date": # order add
if(!preg_match("/(2[0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9])/", $value))
$this->validation_error = $key . " must be a date in the format YYYY-MM-DD";
break;
case "shipping_carrier": # add shipping status
if(!in_array($value, array("fedex","ups","other")))
$this->validation_error = $key . " must be one of [fedex, ups, other]";
break;
case "shipping_status": # add shipping stauts, update shipping status
if(!in_array($value, array("pickedup", "intransit", "arrived", "delivered")))
$this->validation_error = $key . " must be one of [pickedup, intransit, arrived, delivered]";
break;
case "s_status": # order add
if(!in_array($value, array("pending","cancel","processing","completed")))
$this->validation_error = $key . " must be one of [pending, cancel, processing, completed]";
break;
case "status": # product add/edit
case "department_status":
case "category_status":
if(!in_array($value, array("active","hidden","sold","coming")))
$this->validation_error = "Status must be one of these : [active, hidden, sold, coming]";
break;
case "product_file": # product bulk upload
$value = substr($value, 1);
if(!file_exists($value))
$this->validation_error = $key . " must be a readable file (" . $value . " not readable)";
break;
case "companyLogoUrL":
case "companyWebsite":
case "product_image_url": # product add/update/add_image/update_image
case "video_url": # product add/update
case "gallery_image_url":
# http://phpcentral.com/208-url-validation-in-php.html
if(!preg_match('|^(http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?)?$|i', $value))
$this->validation_error = $key . " must be a well-formed URL";
break;
case "store_id": # global
case "territory_id": # store countries
case "country_id": # store states
case "state_id": # store get/add shipping method
case "department_id": # store get by dept
case "category_id": # store get by sub-dept
case "parent_category_id":
case "f_price": # product add/update
case "discount": # product add/update
case "aggregate_quantity": # product add/update/add_option/update_option
case "image_id":
if(!is_numeric($value))
$this->validation_error = $key . " must be numeric (" . var_export($value,true) . ")";
break;
}
}
}