Guest User

Untitled

a guest
Jun 2nd, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.28 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['retrieve_posts'])) {
  3. $post_ret = $_POST['retrieve_posts'];
  4. }
  5. if(isset($_POST['retrieve_images'])) {
  6. $image_ret = $_POST['retrieve_images'];
  7. } else $image_ret = 0;
  8.  
  9. require_once('classes.php');
  10. require_once('functions.php');
  11.  
  12. if($post_ret == 1) {
  13. $obj = new kigo();
  14. $obj->url = "https://app.kigo.net/api/ra/v1/listProperties";
  15. $obj->user = "bla";
  16. $obj->pass = "bla";
  17. $obj->data = json_encode(null);
  18. $list = $obj->curlkigo();
  19.  
  20. $directory = 'uploads';
  21. $list = $list['API_REPLY'];
  22. $c = count($list);
  23. $kigopropid = array();
  24. for($i=0;$i<$c;$i++) {
  25. $kigopropid[] = $list[$i]['PROP_ID'];
  26. $propname[] = $list[$i]['PROP_NAME'];
  27. }
  28. if(namecheck($kigopropid, $directory)!=null) {
  29. $namecheck = namecheck($kigopropid, $directory);
  30. $tau = 0;
  31. foreach ($namecheck as $key => $prop_id) {
  32. $obj = new kigo();
  33. $obj->url = "https://app.kigo.net/api/ra/v1/readProperty";
  34. $obj->user = "bla";
  35. $obj->pass = "bla";
  36. $obj->data = json_encode(array("PROP_ID" => $prop_id));
  37. $obj->curlkigo();
  38. $data = $obj->curlkigo();
  39.  
  40. //-----------Prop Name
  41. $title = $propname[$tau].'
  42. ';
  43. $tau++;
  44.  
  45. //-----------Adress informations
  46. $strnr = unarr($data, 'PROP_STREETNO');
  47. $addr1 = unarr($data, 'PROP_ADDR1');
  48. $addr2 = unarr($data, 'PROP_ADDR2');
  49. $addr3 = unarr($data, 'PROP_ADDR3');
  50. $aptno = unarr($data, 'PROP_APTNO');
  51. $prop_postcode = unarr($data, 'PROP_POSTCODE');
  52. $prop_city = unarr($data, 'PROP_CITY');
  53. $prop_region = unarr($data, 'PROP_REGION');
  54. $prop_country = unarr($data, 'PROP_COUNTRY');
  55. $prop_lat = unarr($data, 'PROP_LATITUDE');
  56. $prop_long = unarr($data, 'PROP_LONGITUDE');
  57. $prop_axcode = unarr($data, 'PROP_AXSCODE');
  58.  
  59. $adress = '
  60. <div class="adress">
  61. <h2>Adress</h2>
  62. <ul>
  63. <li>Primary Adress: '.$addr1.'</li>
  64. <li>Secondary adress: '. $addr2.'</li>
  65. <li>Tertiary adress: '.$addr3.'</li>
  66. <li>Street number: '. $strnr.'</li>
  67. <li>Apartment number: '. $aptno.'</li>
  68. <li>Postcode: '. $prop_postcode.'</li>
  69. <li>City: '. $prop_city.'</li>
  70. <li>Country: '. $prop_country.'</li>
  71. <li>Latitude: '. $prop_lat.'</li>
  72. <li>Longitude: '. $prop_long.'</li>
  73. </ul>
  74. </div>
  75. ';
  76.  
  77.  
  78.  
  79. //-----------Property descriptions
  80. $name = unarr($data, 'PROP_NAME');
  81. $instant_book = unarr($data, 'PROP_INSTANT_BOOK');
  82. $metadescription = unarr($data, 'PROP_SHORTDESCRIPTION');
  83. $description = unarr($data, 'PROP_DESCRIPTION');
  84. $areadescription = unarr($data, 'PROP_AREADESCRIPTION');
  85.  
  86. $properties = '
  87. <div class="content">
  88. <h2>'. $name.'</h2>
  89. <p>'.format($description).'</p>
  90. </div>
  91. ';
  92.  
  93. //-----------Property details
  94. $prop_bedrooms = unarr($data, 'PROP_BEDROOMS');
  95. $prop_beds = unarr($data, 'PROP_BEDS');
  96. $prop_baths = unarr($data, 'PROP_BATHROOMS');
  97. $prop_toilets = unarr($data, 'PROP_TOILETS');
  98. $prop_size = unarr($data, 'PROP_SIZE').strtolower(unarr($data, 'PROP_SIZE_UNIT'))."s";
  99. $prop_floor = unarr($data, 'PROP_FLOOR');
  100. $prop_elevator = unarr($data, 'PROP_ELEVATOR');
  101.  
  102. $details = '
  103. <div class="propdetails">
  104. <h2>Property details</h2>
  105. <ul>
  106. <li>Bedrooms: '.$prop_bedrooms.'</li>
  107. <li>Beds: '. $prop_beds.'</li>
  108. <li>Baths: '.$prop_baths.'</li>
  109. <li>Toilets: '. $prop_toilets.'</li>
  110. <li>Size: '. $prop_size.'</li>
  111. <li>Floor: '. $prop_floor.'</li>
  112. <li>Elevator: '. $prop_elevator.'</li>
  113. </ul>
  114. </div>
  115. ';
  116.  
  117. //-----------Rates
  118. $nightly_rate_from = unarr($data, 'PROP_RATE_NIGHTLY_FROM');
  119. $nightly_rate_to = unarr($data, 'PROP_RATE_NIGHTLY_TO');
  120. $weekly_rate_from = unarr($data, 'PROP_RATE_WEEKLY_FROM');
  121. $weekly_rate_to = unarr($data, 'PROP_RATE_WEEKLY_TO');
  122. $monthly_rate_from = unarr($data, 'PROP_RATE_MONTHLY_FROM');
  123. $monthly_rate_to = unarr($data, 'PROP_RATE_MONTHLY_TO');
  124. $prop_rate_currency = unarr($data, 'PROP_RATE_CURRENCY');
  125.  
  126. $rates = '
  127. <div class="rates">
  128. <h2>Rates</h2>
  129. <ul>
  130. <li>Nigtly rate from: '.$nightly_rate_from.'</li>
  131. <li>Nightly rate to: '.$nightly_rate_to.'</li>
  132. <li>Weekly rate from: '.$weekly_rate_from.'</li>
  133. <li>Weekly rate to: '.$weekly_rate_to.'</li>
  134. <li>Montly rate from: '.$monthly_rate_from.'</li>
  135. <li>Montly rate to: '.$monthly_rate_to.'</li>
  136. <li>Rate currency: '.$prop_rate_currency.'</li>
  137. </div>
  138. ';
  139.  
  140. //-----------Contact
  141. $prop_phone = unarr($data, 'PROP_PHONE');
  142. if($prop_phone==null) {$prop_phone = " - ";}
  143. $contact = '
  144. <div class="contact">
  145. <h2>Contact</h2>
  146. <p>'.$prop_phone.'</p>
  147. </div>
  148. ';
  149.  
  150. if($image_ret==2) {
  151. //-----------Property Images
  152. $prop_array_img = unarr($data, 'PROP_PHOTOS');
  153. $img_ct = count($prop_array_img);
  154. $year = date('Y'); $month = date('m');
  155. for($i=0;$i<$img_ct;$i++) {
  156. $photo_id = $prop_array_img[$i]['PHOTO_ID'];
  157. $obj = new kigo();
  158. $obj->url = "https://app.kigo.net/api/ra/v1/readPropertyPhotoFile";
  159. $obj->user = 'bla';
  160. $obj->pass = 'bla';
  161. $obj->data = json_encode(array("PROP_ID" => $prop_id, "PHOTO_ID"=>$photo_id));
  162. $img = $obj->curlkigo();
  163. $img = str_replace(' ', '+', $img['API_REPLY']);
  164. $data = base64_decode($img);
  165. $file ="../../uploads/".$year."/".$month."/".uniqid() . '.jpg';
  166. $success = file_put_contents($file, $data);
  167. }
  168. echo "The Images were automatically added in media files!";
  169. }
  170.  
  171. $final = $title.$adress.$details.$rates.$properties.$contact;
  172. $create = fopen($directory.'/'.$prop_id.'.txt', 'w+');
  173. $put = file_put_contents($directory.'/'.$prop_id.'.txt', $final);
  174. }//end for
  175. }//end if
  176.  
  177. $filenames = listfiles($directory);
  178. if (file_exists($directory.'/archive.txt')) {
  179. $filenames = array_values(array_diff($filenames, array('archive.txt')));
  180. }
  181. $pathtozipfiles = array();
  182. foreach ($filenames as $value) {
  183. $pathtozipfiles[] = $directory.'/'.$value;
  184. }
  185. $result = create_zip($pathtozipfiles,'articles.zip');
  186. echo $dir;
  187. }//end post
  188. ?>
  189.  
  190. <?php
  191.  
  192. // i have moved the requires to the top, as your class should fail as early as possible if these files don't exist
  193. require_once('classes.php');
  194. require_once('functions.php');
  195.  
  196. // these constants should be stored in a separate config file
  197. const KIGO_USER = 'blabla';
  198. const KIGO_PASS = 'blabla';
  199. const DIR_UPLOADS = 'uploads';
  200.  
  201.  
  202. //if(isset($_POST['retrieve_posts'])) {
  203. // $post_ret = $_POST['retrieve_posts'];
  204. //}
  205.  
  206. // at this point $post_ret may not be set and will cause and error below when you test it
  207. // a nicer way is to use the ternary operator to ensure it has a default value
  208. $post_ret = isset($_POST['retrieve_posts']) ? $_POST['retrieve_posts'] : 0;
  209.  
  210.  
  211. //if(isset($_POST['retrieve_images'])) {
  212. // $image_ret = $_POST['retrieve_images'];
  213. //} else $image_ret = 0;
  214.  
  215. // same here with ternary operator
  216. $image_ret = isset($_POST['retrieve_images']) ? $_POST['retrieve_images'] : 0;
  217.  
  218.  
  219.  
  220. if($post_ret == 1) {
  221.  
  222. // the url, user, pass should all be constants or put into a helper class of sorts,
  223. // see mykigo helper class below
  224. // $obj = new kigo();
  225. // $obj->url = "https://app.kigo.net/api/ra/v1/listProperties";
  226. // $obj->user = "bla";
  227. // $obj->pass = "bla";
  228. // $obj->data = json_encode(null);
  229.  
  230. $mykigo = new mykigo(KIGO_USER, KIGO_PASS);
  231.  
  232. // directory looks like config data and should be stored out of the main code unless it changes often
  233. // see const DIR_UPLOADS above
  234. // $directory = 'uploads';
  235. // $list = $list['API_REPLY'];
  236. $list = $mykigo->listProperties();
  237.  
  238. $kigopropid = array();
  239.  
  240. // $c = count($list);
  241. // for($i=0;$i<$c;$i++) {
  242.  
  243. // i know people will say to do the count first so it is optimized better, but in reality count($list) will take such a tiny amount of time,
  244. // that i prefer it like this, as i find it more readable.
  245. for($i=0; $i<count($list); $i++) {
  246. $kigopropid[] = $list[$i]['PROP_ID'];
  247. $propname[] = $list[$i]['PROP_NAME'];
  248. }
  249.  
  250. // namecheck is not a very obvious description of what the function does
  251. // for example, would something like this be more appropriate
  252. // if(is_directory_valid($kigopropid, $directory)!=null) {
  253.  
  254. // here we are doing namecheck twice, what for?
  255. // if(namecheck($kigopropid, $directory)!=null) {
  256. // $namecheck = namecheck($kigopropid, $directory);
  257.  
  258. // I am not sure what namecheck actually does, but this might be better
  259. $namecheck = namecheck($kigopropid, DIR_UPLOADS);
  260. if($namecheck != null) {
  261.  
  262. $tau = 0;
  263.  
  264. foreach ($namecheck as $key => $prop_id) {
  265.  
  266. // $obj is such a generic name, it would be nicer to give it a name that means something
  267. // eg $kigo = new kigo();
  268. // $obj = new kigo();
  269. // $obj->url = "https://app.kigo.net/api/ra/v1/readProperty";
  270. //
  271. // // i hope you haven't just posted your real username/password to the world
  272. // $obj->user = "xxx";
  273. // $obj->pass = "xxx";
  274. //
  275. // $obj->data = json_encode(array("PROP_ID" => $prop_id));
  276. //
  277. // // why do we call this twice, i am guessing it is a mistake?
  278. // $obj->curlkigo();
  279. // $data = $obj->curlkigo();
  280.  
  281. // don't need to instantiate mykigo again, we have done that already, and can reuse it
  282. $data = $mykigo->readProperty($prop_id);
  283.  
  284. //-----------Prop Name
  285. $title = $propname[$tau].'
  286. ';
  287. $tau++;
  288.  
  289. // this code should be split into function, see below
  290. // i have done this one for example, I will leave the rest for you to do
  291. // by splitting into functions you will have a clear concise main function,
  292. // that should be easily readable
  293.  
  294. $adress = render_address_to_html($data);
  295.  
  296. // //-----------Adress informations
  297. // $strnr = unarr($data, 'PROP_STREETNO');
  298. // $addr1 = unarr($data, 'PROP_ADDR1');
  299. // $addr2 = unarr($data, 'PROP_ADDR2');
  300. // $addr3 = unarr($data, 'PROP_ADDR3');
  301. // $aptno = unarr($data, 'PROP_APTNO');
  302. // $prop_postcode = unarr($data, 'PROP_POSTCODE');
  303. // $prop_city = unarr($data, 'PROP_CITY');
  304. // $prop_region = unarr($data, 'PROP_REGION');
  305. // $prop_country = unarr($data, 'PROP_COUNTRY');
  306. // $prop_lat = unarr($data, 'PROP_LATITUDE');
  307. // $prop_long = unarr($data, 'PROP_LONGITUDE');
  308. // $prop_axcode = unarr($data, 'PROP_AXSCODE');
  309. //
  310. // $adress = '
  311. // <div class="adress">
  312. // <h2>Adress</h2>
  313. // <ul>
  314. // <li>Primary Adress: '.$addr1.'</li>
  315. // <li>Secondary adress: '. $addr2.'</li>
  316. // <li>Tertiary adress: '.$addr3.'</li>
  317. // <li>Street number: '. $strnr.'</li>
  318. // <li>Apartment number: '. $aptno.'</li>
  319. // <li>Postcode: '. $prop_postcode.'</li>
  320. // <li>City: '. $prop_city.'</li>
  321. // <li>Country: '. $prop_country.'</li>
  322. // <li>Latitude: '. $prop_lat.'</li>
  323. // <li>Longitude: '. $prop_long.'</li>
  324. // </ul>
  325. // </div>
  326. // ';
  327.  
  328.  
  329.  
  330. //-----------Property descriptions
  331. $name = unarr($data, 'PROP_NAME');
  332. $instant_book = unarr($data, 'PROP_INSTANT_BOOK');
  333. $metadescription = unarr($data, 'PROP_SHORTDESCRIPTION');
  334. $description = unarr($data, 'PROP_DESCRIPTION');
  335. $areadescription = unarr($data, 'PROP_AREADESCRIPTION');
  336.  
  337. $properties = '
  338. <div class="content">
  339. <h2>'. $name.'</h2>
  340. <p>'.format($description).'</p>
  341. </div>
  342. ';
  343.  
  344. //-----------Property details
  345. $prop_bedrooms = unarr($data, 'PROP_BEDROOMS');
  346. $prop_beds = unarr($data, 'PROP_BEDS');
  347. $prop_baths = unarr($data, 'PROP_BATHROOMS');
  348. $prop_toilets = unarr($data, 'PROP_TOILETS');
  349. $prop_size = unarr($data, 'PROP_SIZE').strtolower(unarr($data, 'PROP_SIZE_UNIT'))."s";
  350. $prop_floor = unarr($data, 'PROP_FLOOR');
  351. $prop_elevator = unarr($data, 'PROP_ELEVATOR');
  352.  
  353. $details = '
  354. <div class="propdetails">
  355. <h2>Property details</h2>
  356. <ul>
  357. <li>Bedrooms: '.$prop_bedrooms.'</li>
  358. <li>Beds: '. $prop_beds.'</li>
  359. <li>Baths: '.$prop_baths.'</li>
  360. <li>Toilets: '. $prop_toilets.'</li>
  361. <li>Size: '. $prop_size.'</li>
  362. <li>Floor: '. $prop_floor.'</li>
  363. <li>Elevator: '. $prop_elevator.'</li>
  364. </ul>
  365. </div>
  366. ';
  367.  
  368. //-----------Rates
  369. $nightly_rate_from = unarr($data, 'PROP_RATE_NIGHTLY_FROM');
  370. $nightly_rate_to = unarr($data, 'PROP_RATE_NIGHTLY_TO');
  371. $weekly_rate_from = unarr($data, 'PROP_RATE_WEEKLY_FROM');
  372. $weekly_rate_to = unarr($data, 'PROP_RATE_WEEKLY_TO');
  373. $monthly_rate_from = unarr($data, 'PROP_RATE_MONTHLY_FROM');
  374. $monthly_rate_to = unarr($data, 'PROP_RATE_MONTHLY_TO');
  375. $prop_rate_currency = unarr($data, 'PROP_RATE_CURRENCY');
  376.  
  377. $rates = '
  378. <div class="rates">
  379. <h2>Rates</h2>
  380. <ul>
  381. <li>Nigtly rate from: '.$nightly_rate_from.'</li>
  382. <li>Nightly rate to: '.$nightly_rate_to.'</li>
  383. <li>Weekly rate from: '.$weekly_rate_from.'</li>
  384. <li>Weekly rate to: '.$weekly_rate_to.'</li>
  385. <li>Montly rate from: '.$monthly_rate_from.'</li>
  386. <li>Montly rate to: '.$monthly_rate_to.'</li>
  387. <li>Rate currency: '.$prop_rate_currency.'</li>
  388. </div>
  389. ';
  390.  
  391. //-----------Contact
  392. $prop_phone = unarr($data, 'PROP_PHONE');
  393. if($prop_phone==null) {$prop_phone = " - ";}
  394. $contact = '
  395. <div class="contact">
  396. <h2>Contact</h2>
  397. <p>'.$prop_phone.'</p>
  398. </div>
  399. ';
  400.  
  401. if($image_ret==2) {
  402. //-----------Property Images
  403. $prop_array_img = unarr($data, 'PROP_PHOTOS');
  404. $img_ct = count($prop_array_img);
  405. $year = date('Y'); $month = date('m');
  406. for($i=0;$i<$img_ct;$i++) {
  407. $photo_id = $prop_array_img[$i]['PHOTO_ID'];
  408.  
  409. // moved into mykigo helper class
  410. $data = $mykigo->readPropertyPhotoFile($prop_id, $photo_id);
  411.  
  412. // $obj = new kigo();
  413. // $obj->url = "https://app.kigo.net/api/ra/v1/readPropertyPhotoFile";
  414. // $obj->user = "xxx";
  415. // $obj->pass = "xxx";
  416. // $obj->data = json_encode(array("PROP_ID" => $prop_id, "PHOTO_ID"=>$photo_id));
  417. // $img = $obj->curlkigo();
  418. // $img = str_replace(' ', '+', $img['API_REPLY']);
  419. // $data = base64_decode($img);
  420.  
  421.  
  422. $file ="../../uploads/".$year."/".$month."/".uniqid() . '.jpg';
  423. $success = file_put_contents($file, $data);
  424. }
  425. echo "The Images were automatically added in media files!";
  426. }
  427.  
  428. $final = $title.$adress.$details.$rates.$properties.$contact;
  429. $create = fopen(DIR_UPLOADS.'/'.$prop_id.'.txt', 'w+');
  430. $put = file_put_contents(DIR_UPLOADS.'/'.$prop_id.'.txt', $final);
  431. }//end for
  432. }//end if
  433.  
  434. $filenames = listfiles(DIR_UPLOADS);
  435. if (file_exists(DIR_UPLOADS.'/archive.txt')) {
  436. $filenames = array_values(array_diff($filenames, array('archive.txt')));
  437. }
  438. $pathtozipfiles = array();
  439. foreach ($filenames as $value) {
  440. $pathtozipfiles[] = DIR_UPLOADS.'/'.$value;
  441. }
  442. $result = create_zip($pathtozipfiles,'articles.zip');
  443. echo $dir;
  444.  
  445.  
  446. function render_address_to_html($data) {
  447.  
  448. //-----------Adress informations
  449. $strnr = unarr($data, 'PROP_STREETNO');
  450. $addr1 = unarr($data, 'PROP_ADDR1');
  451. $addr2 = unarr($data, 'PROP_ADDR2');
  452. $addr3 = unarr($data, 'PROP_ADDR3');
  453. $aptno = unarr($data, 'PROP_APTNO');
  454. $prop_postcode = unarr($data, 'PROP_POSTCODE');
  455. $prop_city = unarr($data, 'PROP_CITY');
  456. $prop_region = unarr($data, 'PROP_REGION');
  457. $prop_country = unarr($data, 'PROP_COUNTRY');
  458. $prop_lat = unarr($data, 'PROP_LATITUDE');
  459. $prop_long = unarr($data, 'PROP_LONGITUDE');
  460. $prop_axcode = unarr($data, 'PROP_AXSCODE');
  461.  
  462. $adress = '
  463. <div class="adress">
  464. <h2>Adress</h2>
  465. <ul>
  466. <li>Primary Adress: '.$addr1.'</li>
  467. <li>Secondary adress: '. $addr2.'</li>
  468. <li>Tertiary adress: '.$addr3.'</li>
  469. <li>Street number: '. $strnr.'</li>
  470. <li>Apartment number: '. $aptno.'</li>
  471. <li>Postcode: '. $prop_postcode.'</li>
  472. <li>City: '. $prop_city.'</li>
  473. <li>Country: '. $prop_country.'</li>
  474. <li>Latitude: '. $prop_lat.'</li>
  475. <li>Longitude: '. $prop_long.'</li>
  476. </ul>
  477. </div>
  478. ';
  479.  
  480. return $adress;
  481. }
  482.  
  483.  
  484. }//end post
  485.  
  486. // i always remove the ending php closure, it has no use, and can actually mask whitespace getting output to page,
  487. // which can effect the setting of cookies, etc
  488.  
  489. private class mykigo extends kigo {
  490.  
  491. const URL_PROPERTY_PHOTO_FILE = 'https://app.kigo.net/api/ra/v1/readPropertyPhotoFile';
  492. const URL_LIST_PROPERTIES = 'https://app.kigo.net/api/ra/v1/listProperties';
  493. const URL_READ_PROPERTY = "https://app.kigo.net/api/ra/v1/readProperty";
  494.  
  495.  
  496. function __construct($user, $pass) {
  497. parent::__construct();
  498.  
  499. $this->user = $user;
  500. $this->pass = $pass;
  501. }
  502.  
  503. public function readProperty($prop_id) {
  504. $this->url = self::URL_READ_PROPERTY;
  505. $this->data = json_encode(array("PROP_ID" => $prop_id));
  506. return $this->curlkigo();
  507. }
  508.  
  509. public function listProperties() {
  510. $this->url = self::URL_LIST_PROPERTIES;
  511. $this->data = json_encode(null);
  512. $ret = $this->curlkigo();
  513. return $ret['API_REPLY'];
  514. }
  515.  
  516. public function readPropertyPhotoFile($prop_id, $photo_id) {
  517. $this->url = self::URL_PROPERTY_PHOTO_FILE;
  518. $this->data = json_encode(array("PROP_ID" => $prop_id, "PHOTO_ID"=>$photo_id));
  519. $ret = $this->curlkigo();
  520. $img = str_replace(' ', '+', $ret['API_REPLY']);
  521.  
  522. return base64_decode($img);
  523. }
  524. }
Add Comment
Please, Sign In to add comment