PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-includes/osclass/install-location.php

https://github.com/nsswaga/OSClass
PHP | 418 lines | 341 code | 75 blank | 2 comment | 59 complexity | 5b449377e737f96aacefcf643065a639 MD5 | raw file
  1. <?php
  2. //error_reporting(E_ALL);
  3. error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_PARSE);
  4. define( 'ABS_PATH', dirname(dirname(dirname(__FILE__))) . '/' );
  5. define( 'LIB_PATH', ABS_PATH . 'oc-includes/');
  6. require_once ABS_PATH . 'config.php';
  7. require_once LIB_PATH . 'osclass/db.php';
  8. require_once LIB_PATH . 'osclass/classes/DAO.php';
  9. require_once LIB_PATH . 'osclass/helpers/hDatabaseInfo.php';
  10. require_once LIB_PATH . 'osclass/install-functions.php';
  11. require_once LIB_PATH . 'osclass/formatting.php';
  12. require_once LIB_PATH . 'osclass/compatibility.php';
  13. require_once LIB_PATH . 'osclass/utils.php';
  14. require_once LIB_PATH . 'osclass/helpers/hPreference.php' ;
  15. require_once LIB_PATH . 'osclass/Logger/Logger.php' ;
  16. require_once LIB_PATH . 'osclass/Logger/LogOsclass.php' ;
  17. $_POST = add_slashes_extended($_POST) ;
  18. if( is_osclass_installed() ) {
  19. die() ;
  20. }
  21. $json_message = array();
  22. $json_message['status'] = '200';
  23. $result = basic_info();
  24. $json_message['email_status'] = $result['email_status'];
  25. $json_message['password'] = $result['s_password'];
  26. if( $_POST['skip-location-h'] == 0 ) {
  27. $msg = install_locations() ;
  28. $json_message['status'] = $msg;
  29. }
  30. echo json_encode($json_message);
  31. function basic_info() {
  32. require_once LIB_PATH . 'osclass/model/Admin.php' ;
  33. require_once LIB_PATH . 'osclass/model/Preference.php' ;
  34. require_once LIB_PATH . 'osclass/helpers/hSecurity.php' ;
  35. if($_POST['s_name'] == ''){
  36. $admin = 'admin';
  37. } else { $admin = $_POST['s_name']; }
  38. if($_POST['s_passwd'] == ''){
  39. $password = osc_genRandomPassword() ;
  40. } else { $password = $_POST['s_passwd']; }
  41. Admin::newInstance()->insert(
  42. array(
  43. 's_name' => 'Administrator'
  44. ,'s_username' => $admin
  45. ,'s_password' => sha1($password)
  46. ,'s_email' => $_POST['email']
  47. )
  48. ) ;
  49. $mPreference = Preference::newInstance() ;
  50. $mPreference->insert (
  51. array(
  52. 's_section' => 'osclass'
  53. ,'s_name' => 'pageTitle'
  54. ,'s_value' => $_POST['webtitle']
  55. ,'e_type' => 'STRING'
  56. )
  57. ) ;
  58. $mPreference->insert (
  59. array(
  60. 's_section' => 'osclass'
  61. ,'s_name' => 'contactEmail'
  62. ,'s_value' => $_POST['email']
  63. ,'e_type' => 'STRING'
  64. )
  65. ) ;
  66. $body = 'Welcome ' . $_POST['webtitle'] . ',<br/><br/>' ;
  67. $body .= 'Your OSClass installation at ' . WEB_PATH . ' is up and running. You can access to the administration panel with this data access:<br/>' ;
  68. $body .= '<ul>' ;
  69. $body .= '<li>username: ' . $admin . '</li>' ;
  70. $body .= '<li>password: ' . $password . '</li>' ;
  71. $body .= '</ul>' ;
  72. $body .= 'Regards,<br/>' ;
  73. $body .= 'The <a href=\'http://osclass.org/\'>OSClass</a> team' ;
  74. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  75. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  76. $sitename = substr( $sitename, 4 ) ;
  77. }
  78. try{
  79. require_once LIB_PATH . 'phpmailer/class.phpmailer.php' ;
  80. $mail = new PHPMailer(true) ;
  81. $mail->CharSet="utf-8" ;
  82. $mail->Host = "localhost" ;
  83. $mail->From = 'osclass@' . $sitename ;
  84. $mail->FromName = 'OSClass' ;
  85. $mail->Subject = 'OSClass successfully installed!' ;
  86. $mail->AddAddress($_POST['email'], 'OSClass administrator') ;
  87. $mail->Body = $body ;
  88. $mail->AltBody = $body ;
  89. if (!$mail->Send()) {
  90. return array('email_status' => $_POST['email']."<br>".$mail->ErrorInfo,
  91. 's_password' => $password );
  92. }else{
  93. return array('email_status' => '',
  94. 's_password' => $password );
  95. }
  96. }catch(phpmailerException $exception) {
  97. }
  98. }
  99. function location_international() {
  100. $manager_country = Country::newInstance();
  101. $manager_region = Region::newInstance();
  102. $manager_city = City::newInstance();
  103. $countries_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=country&term=all&install=true&version='.osc_version());
  104. $countries = json_decode($countries_json);
  105. if( count($countries) == 0 ) {
  106. if (reportToOsclass()){
  107. LogOsclassInstaller::instance()->error('Cannot get countries' , __FILE__."::".__LINE__) ;
  108. }
  109. return '300';
  110. }
  111. foreach($countries as $c) {
  112. $manager_country->insert(array(
  113. "pk_c_code" => $c->id,
  114. "fk_c_locale_code" => $c->locale_code,
  115. "s_name" => $c->name
  116. )) ;
  117. }
  118. $regions_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=region&country=all&term=all');
  119. $regions = json_decode($regions_json);
  120. if( count($regions) == 0 && reportToOsclass()){
  121. LogOsclassInstaller::instance()->error('Cannot get regions' , __FILE__."::".__LINE__) ;
  122. }
  123. foreach($regions as $r) {
  124. $manager_region->insert(array(
  125. "pk_i_id" => $r->id,
  126. "fk_c_country_code" => $r->country_code,
  127. "s_name" => $r->name
  128. ));
  129. }
  130. foreach($countries as $c) {
  131. $cities_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=city&country=' . urlencode($c->name) . '&term=all');
  132. $cities = json_decode($cities_json);
  133. if(!isset($cities->error)) {
  134. foreach($cities as $ci) {
  135. $manager_city->insert(array(
  136. "pk_i_id" => $ci->id,
  137. "fk_i_region_id" => $ci->region_id,
  138. "s_name" => $ci->name,
  139. "fk_c_country_code" => $ci->country_code
  140. ));
  141. }
  142. } else {
  143. if( reportToOsclass() ){
  144. LogOsclassInstaller::instance()->error('Cannot get cities by country ' . $c->name , __FILE__."::".__LINE__) ;
  145. }
  146. }
  147. unset($cities);
  148. unset($cities_json);
  149. }
  150. return '200';
  151. }
  152. function location_by_country() {
  153. if(!isset($_POST['country']))
  154. return false;
  155. $country = $_POST['country'];
  156. $countries_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=country&term='. urlencode(implode(',', $country)) . '&install=true&version='.osc_version() );
  157. $countries = json_decode($countries_json);
  158. $manager_country = Country::newInstance();
  159. if( count($countries) == 0 ) {
  160. if( reportToOsclass() ){
  161. LogOsclassInstaller::instance()->error('Cannot get countries - ' . implode(',', $country) , __FILE__."::".__LINE__) ;
  162. }
  163. return '300';
  164. }
  165. foreach($countries as $c) {
  166. $manager_country->insert(array(
  167. "pk_c_code" => $c->id,
  168. "fk_c_locale_code" => $c->locale_code,
  169. "s_name" => $c->name
  170. ));
  171. }
  172. $manager_region = Region::newInstance();
  173. $regions_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=region&country=' . urlencode(implode(',', $country)) . '&term=all');
  174. $regions = json_decode($regions_json);
  175. if( count($regions) == 0 && reportToOsclass()){
  176. LogOsclassInstaller::instance()->error('Cannot get regions by - ' . implode(',', $country) , __FILE__."::".__LINE__) ;
  177. }
  178. foreach($regions as $r) {
  179. $manager_region->insert(array(
  180. "pk_i_id" => $r->id,
  181. "fk_c_country_code" => $r->country_code,
  182. "s_name" => $r->name
  183. ));
  184. }
  185. $manager_city = City::newInstance();
  186. foreach($countries as $c) {
  187. $cities_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=city&country=' . urlencode($c->name) . '&term=all');
  188. $cities = json_decode($cities_json);
  189. if(!isset($cities->error)) {
  190. foreach($cities as $ci) {
  191. $manager_city->insert(array(
  192. "pk_i_id" => $ci->id,
  193. "fk_i_region_id" => $ci->region_id,
  194. "s_name" => $ci->name,
  195. "fk_c_country_code" => $ci->country_code
  196. ));
  197. }
  198. } else {
  199. if( reportToOsclass() ) {
  200. LogOsclassInstaller::instance()->error('Cannot get cities by country - ' . $c->name , __FILE__."::".__LINE__) ;
  201. }
  202. }
  203. unset($cities);
  204. unset($cities_json);
  205. }
  206. return '200';
  207. }
  208. function location_by_region() {
  209. if(!isset($_POST['country']))
  210. return false;
  211. if(!isset($_POST['region']))
  212. return false;
  213. $country = $_POST['country'];
  214. $region = $_POST['region'];
  215. $countries_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=country&term=' . urlencode(implode(',', $country)) . '&install=true&version='.osc_version() );
  216. $countries = json_decode($countries_json);
  217. $manager_country = Country::newInstance();
  218. if( count($countries) == 0 ) {
  219. if( reportToOsclass() ){
  220. LogOsclassInstaller::instance()->error('Cannot get countries - ' . implode(',', $country) , __FILE__."::".__LINE__) ;
  221. }
  222. return '300';
  223. }
  224. foreach($countries as $c) {
  225. $manager_country->insert(array(
  226. "pk_c_code" => $c->id,
  227. "fk_c_locale_code" => $c->locale_code,
  228. "s_name" => $c->name
  229. ));
  230. }
  231. $regions_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=region&country=' . urlencode(implode(',', $country)) . '&term=' . urlencode(implode(',', $region)));
  232. $regions = json_decode($regions_json);
  233. $manager_region = Region::newInstance();
  234. if( count($regions) == 0 ) {
  235. if( reportToOsclass() ){
  236. LogOsclassInstaller::instance()->error('Cannot get regions - ' . implode(',', $country) .'- term' . implode(',', $region) , __FILE__."::".__LINE__) ;
  237. }
  238. return '300';
  239. }
  240. foreach($regions as $r) {
  241. $manager_region->insert(array(
  242. "pk_i_id" => $r->id,
  243. "fk_c_country_code" => $r->country_code,
  244. "s_name" => $r->name
  245. ));
  246. }
  247. $manager_city = City::newInstance();
  248. foreach($countries as $c) {
  249. $cities_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=city&country=' . urlencode($c->name) . '&region=' . urlencode(implode(',', $region)) . '&term=');
  250. $cities = json_decode($cities_json);
  251. if(!isset($cities->error)) {
  252. foreach($cities as $ci) {
  253. $manager_city->insert(array(
  254. "pk_i_id" => $ci->id,
  255. "fk_i_region_id" => $ci->region_id,
  256. "s_name" => $ci->name,
  257. "fk_c_country_code" => $ci->country_code
  258. ));
  259. }
  260. } else {
  261. if( reportToOsclass() ){
  262. LogOsclassInstaller::instance()->error('Cannot get regions by country - ' . $c->name .'- by region ' . implode(',', $region) , __FILE__."::".__LINE__) ;
  263. }
  264. }
  265. unset($cities);
  266. unset($cities_json);
  267. }
  268. return '200';
  269. }
  270. function location_by_city() {
  271. if(!isset($_POST['country']))
  272. return false;
  273. if(!isset($_POST['city']))
  274. return false;
  275. $country = $_POST['country'];
  276. $city = $_POST['city'];
  277. $countries_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=country&term='. urlencode(implode(',', $country)) . '&install=true&version='.osc_version() );
  278. $countries = json_decode($countries_json);
  279. $manager_country = Country::newInstance();
  280. if( count($countries) == 0 && reportToOsclass()){
  281. LogOsclassInstaller::instance()->error('Cannot get countries - ' . implode(',', $country) , __FILE__."::".__LINE__) ;
  282. }
  283. foreach($countries as $c) {
  284. $manager_country->insert(array(
  285. "pk_c_code" => $c->id,
  286. "fk_c_locale_code" => $c->locale_code,
  287. "s_name" => $c->name
  288. ));
  289. }
  290. $manager_city = City::newInstance();
  291. $manager_region = Region::newInstance();
  292. foreach($countries as $c) {
  293. $cities_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=city&country=' . urlencode($c->name) . '&term=' . urlencode(implode(',', $city)));
  294. $cities = json_decode($cities_json);
  295. if(!isset($cities->error)) {
  296. foreach($cities as $ci) {
  297. $regions_json = osc_file_get_contents('http://geo.osclass.org/geo.download.php?action=region&country=&id=' . $ci->region_id);
  298. $regions = json_decode($regions_json);
  299. if( count($regions) == 0 && reportToOsclass() ){
  300. LogOsclassInstaller::instance()->error('Cannot get regions by - ' .$ci->region_id , __FILE__."::".__LINE__) ;
  301. }
  302. foreach($regions as $r) {
  303. $manager_region->insert(array(
  304. "pk_i_id" => $r->id,
  305. "fk_c_country_code" => $r->country_code,
  306. "s_name" => $r->name
  307. ));
  308. }
  309. $manager_city->insert(array(
  310. "pk_i_id" => $ci->id,
  311. "fk_i_region_id" => $ci->region_id,
  312. "s_name" => $ci->name,
  313. "fk_c_country_code" => $ci->country_code
  314. ));
  315. }
  316. } else {
  317. if( reportToOsclass() ){
  318. LogOsclassInstaller::instance()->error('Cannot get cities by - ' . $c->name . ' - term ' . implode(',', $city) , __FILE__."::".__LINE__) ;
  319. }
  320. return '300';
  321. }
  322. unset($cities);
  323. unset($cities_json);
  324. }
  325. return '200';
  326. }
  327. function install_locations ( ) {
  328. // first of all we check if is a international or a specific installation
  329. if( !isset($_POST['c_country']) )
  330. return false;
  331. require_once ABS_PATH . 'oc-includes/osclass/model/Country.php';
  332. require_once ABS_PATH . 'oc-includes/osclass/model/Region.php';
  333. require_once ABS_PATH . 'oc-includes/osclass/model/City.php';
  334. if( isset($_POST['city']) )
  335. return location_by_city();
  336. else if( isset($_POST['region']) )
  337. return location_by_region();
  338. else if( isset($_POST['country']) )
  339. return location_by_country();
  340. else
  341. return location_international ();
  342. }
  343. ?>