db.php (10230B)
1 <?php 2 class ControllerServiceDb extends Controller { 3 4 public function index() { 5 global $sdb, $secret_key, $returnObj; 6 $this->load->helper('nusoap/nusoap'); 7 $this->load->model('setting/setting'); 8 $sdb = $this->db; 9 $secret_key=''; 10 11 $data = $this->model_setting_setting->getSetting('servicedb'); 12 if (isset($data['servicedb_secret_key'])) { 13 $secret_key = $data['servicedb_secret_key']; 14 } 15 16 $returnObj= new stdClass; 17 $returnObj->secretStatus = false; 18 19 // Create the server instance 20 $server = new soap_server(); 21 // Initialize WSDL support 22 $server->configureWSDL('dbServices', 'urn:dbServices'); 23 // Register the method to expose 24 25 26 $server->register('select', // method name 27 array('sql' => 'xsd:string', 'secret'=>'xsd:string'), // input parameters 28 array('return' => 'xsd:string'), // output parameters 29 'urn:dbServices', // namespace 30 'urn:dbServices#query', // soapaction 31 'rpc', // style 32 'encoded', // use 33 'Run SELECT QUERY to Opencart Database' // documentation 34 ); 35 $server->register('query', // method name 36 array('sql' => 'xsd:string', 'secret'=>'xsd:string'), // input parameters 37 array('return' => 'xsd:string'), // output parameters 38 'urn:dbServices', // namespace 39 'urn:dbServices#query', // soapaction 40 'rpc', // style 41 'encoded', // use 42 'Run INESRT/UPDATE/DELETE Query to Opencart Database' // documentation 43 ); 44 $server->register('lastId', // method name 45 array('secret'=>'xsd:string'), // input parameters 46 array('return' => 'xsd:string'), // output parameters 47 'urn:dbServices', // namespace 48 'urn:dbServices#lastId', // soapaction 49 'rpc', // style 50 'encoded', // use 51 'Return Last Inserted ID' // documentation 52 ); 53 $server->register('countAffected', // method name 54 array('secret'=>'xsd:string'), // input parameters 55 array('return' => 'xsd:string'), // output parameters 56 'urn:dbServices', // namespace 57 'urn:dbServices#countAffected', // soapaction 58 'rpc', // style 59 'encoded', // use 60 'Return count of affected rows' // documentation 61 ); 62 $server->register('getTablePrefix', // method name 63 array('secret'=>'xsd:string'), // input parameters 64 array('return' => 'xsd:string'), // output parameters 65 'urn:dbServices', // namespace 66 'urn:dbServices#getTablePrefix', // soapaction 67 'rpc', // style 68 'encoded', // use 69 'Return Prefix' // documentation 70 ); 71 $server->register('getDatabaseType', // method name 72 array('secret'=>'xsd:string'), // input parameters 73 array('return' => 'xsd:string'), // output parameters 74 'urn:dbServices', // namespace 75 'urn:dbServices#getDatabaseType', // soapaction 76 'rpc', // style 77 'encoded', // use 78 'Return type of database' // documentation 79 ); 80 $server->register('getOCVersion', // method name 81 array('secret'=>'xsd:string'), // input parameters 82 array('return' => 'xsd:string'), // output parameters 83 'urn:dbServices', // namespace 84 'urn:dbServices#getOCVersion', // soapaction 85 'rpc', // style 86 'encoded', // use 87 'Return version of OC' // documentation 88 ); 89 $server->register('getErrorLog', // method name 90 array('secret'=>'xsd:string'), // input parameters 91 array('return' => 'xsd:string'), // output parameters 92 'urn:dbServices', // namespace 93 'urn:dbServices#getDatabaseType', // soapaction 94 'rpc', // style 95 'encoded', // use 96 'Return type of database' // documentation 97 ); 98 $server->register('addProductImage', // method name 99 array('secret'=>'xsd:string', 100 'productId'=>'xsd:integer', 101 'imageName'=>'xsd:imageName', 102 'imageData'=>'imageData', 103 ), 104 array('return' => 'xsd:string'), 105 'urn:dbServices', 106 'urn:dbServices#addProductImage', 107 'rpc', 108 'encoded', 109 'Upload Product Image' 110 ); 111 112 113 $postdata = file_get_contents("php://input"); 114 $server->service($postdata); 115 116 } 117 118 } 119 function select($sql, $secret) { 120 global $sdb, $secret_key, $returnObj; 121 122 if($secret == $secret_key && !empty($secret_key)) { 123 $sql = base64_decode($sql); 124 $escaped = $sdb->escape($sql); 125 //$result = $sdb->query($escaped); 126 $result = $sdb->query($sql); 127 $returnObj->secretStatus = true; 128 $returnObj->result = $result->rows; 129 } else { 130 $returnObj->result = array(); 131 } 132 return json_encode($returnObj); 133 } 134 135 function query($sql, $secret) { 136 global $sdb, $secret_key, $returnObj; 137 if($secret == $secret_key && !empty($secret_key)) { 138 $sql = base64_decode($sql); 139 $escaped = $sdb->escape($sql); 140 //$result = $sdb->query($escaped); 141 //$result = $sdb->query($sql); 142 $qArr = explode("|", $sql); 143 foreach ($qArr as $qArrItem) {$sdb->query($qArrItem);} 144 $returnObj->secretStatus = true; 145 $returnObj->result = true; 146 } else { 147 $returnObj->result = false; 148 } 149 return json_encode($returnObj); 150 } 151 152 function lastId($secret) { 153 global $sdb, $secret_key, $returnObj; 154 if($secret == $secret_key && !empty($secret_key)) { 155 $result = $sdb->getLastId(); 156 $returnObj->secretStatus = true; 157 $returnObj->result = $result; 158 } else { 159 $returnObj->result = false; 160 } 161 return json_encode($returnObj); 162 } 163 164 function countAffected($secret) { 165 global $sdb, $secret_key, $returnObj; 166 if($secret == $secret_key && !empty($secret_key)) { 167 $result = $sdb->countAffected(); 168 $returnObj->secretStatus = true; 169 $returnObj->result = $result; 170 } else { 171 $returnObj->result = false; 172 } 173 return json_encode($returnObj); 174 } 175 176 function getTablePrefix($secret) { 177 global $sdb, $secret_key, $returnObj; 178 if($secret == $secret_key && !empty($secret_key)) { 179 $returnObj->secretStatus = true; 180 $returnObj->result = DB_PREFIX; 181 } else { 182 $returnObj->result = false; 183 } 184 return json_encode($returnObj); 185 } 186 187 function getDatabaseType($secret) { 188 global $sdb, $secret_key, $returnObj; 189 if($secret == $secret_key && !empty($secret_key)) { 190 $returnObj->secretStatus = true; 191 $returnObj->result = DB_DRIVER; 192 } else { 193 $returnObj->result = false; 194 } 195 return json_encode($returnObj); 196 } 197 function getOCversion($secret) { 198 global $sdb, $secret_key, $returnObj; 199 if($secret == $secret_key && !empty($secret_key)) { 200 $returnObj->secretStatus = true; 201 $returnObj->result = VERSION; 202 } else { 203 $returnObj->result = false; 204 } 205 return json_encode($returnObj); 206 } 207 function getErrorLog($secret) { 208 global $sdb, $secret_key, $returnObj; 209 if($secret == $secret_key && !empty($secret_key)) { 210 $returnObj->secretStatus = true; 211 $returnObj->result = file_get_contents(DIR_LOGS."/error.txt"); 212 } else { 213 $returnObj->result = false; 214 } 215 return json_encode($returnObj); 216 } 217 218 function addProductImage($secret, $productId, $imageName, $imageData) { 219 global $sdb, $secret_key, $returnObj; 220 if($secret == $secret_key && !empty($secret_key)) { 221 $returnObj->secretStatus = true; 222 $productId = intval($productId); 223 $imageName = base64_decode($imageName); 224 $imageData = base64_decode($imageData); 225 //return json_encode($imageData); 226 227 228 if(empty($imageName)) { 229 $returnObj->result = false; 230 $returnObj->resultError = "Невалидно име на снимка!"; 231 } 232 if(!$productId) { 233 $returnObj->result = false; 234 $returnObj->resultError = "Невалидно ИД на продукт"; 235 } 236 //create dirs 237 $directories = explode("/",$imageName); 238 if (count($directories)>1) { 239 array_pop($directories); 240 $path = ''; 241 foreach ($directories as $directory) { 242 $path = $path . '/' . $directory; 243 244 if (!file_exists(DIR_IMAGE . $path)) { 245 @mkdir(DIR_IMAGE . $path, 0777); 246 } 247 } 248 } 249 250 $f = fopen(DIR_IMAGE . $imageName, 'w'); 251 fwrite($f, $imageData); 252 fclose($f); 253 254 //check if is valid image; 255 $info = @getimagesize(DIR_IMAGE . $imageName); 256 if($info === false) { 257 $returnObj->result = false; 258 $returnObj->resultError = "Невалидeн формат на снимката"; 259 @unlink(DIR_IMAGE . $imageName); 260 } else { 261 $sdb->query("UPDATE ".DB_PREFIX."product SET image = '". 262 $sdb->escape($imageName)."' WHERE product_id = '$productId'"); 263 264 $result = $sdb->query("SELECT image FROM ". 265 DB_PREFIX."product_image WHERE product_id = '$productId'"); 266 $productImages = array(); 267 foreach ($result->rows as $row) { 268 $productImages[] = $row['image']; 269 } 270 $productImages[] = $imageName; 271 // delete all records 272 $sdb->query("DELETE FROM ". 273 DB_PREFIX."product_image WHERE product_id = '$productId'"); 274 foreach (array_unique($productImages) as $imgName) { 275 $sdb->query("INSERT INTO ".DB_PREFIX."product_image 276 SET image = '".$sdb->escape($imgName)."' , product_id = '$productId'"); 277 } 278 279 $returnObj->result = true; 280 $returnObj->resultError = ""; 281 } 282 283 } else { 284 $returnObj->result = false; 285 $returnObj->resultError = "Невалиден секретен код"; 286 } 287 return json_encode($returnObj); 288 } 289 290 ?>