AddonController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Addon;
  4. use App\Menu;
  5. use Illuminate\Http\Request;
  6. use ZipArchive;
  7. use DB;
  8. use Auth;
  9. use App\Models\BusinessSetting;
  10. use CoreComponentRepository;
  11. use Illuminate\Support\Str;
  12. use Storage;
  13. use Cache;
  14. class AddonController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. return view('backend.addons.index');
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function create()
  31. {
  32. return view('backend.addons.create');
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. Cache::forget('addons');
  43. if (env('DEMO_MODE') == 'On') {
  44. flash(translate('This action is disabled in demo mode'))->error();
  45. return back();
  46. }
  47. if (class_exists('ZipArchive')) {
  48. if ($request->hasFile('addon_zip')) {
  49. // Create update directory.
  50. $dir = 'addons';
  51. if (!is_dir($dir))
  52. mkdir($dir, 0777, true);
  53. $path = Storage::disk('local')->put('addons', $request->addon_zip);
  54. $zipped_file_name = $request->addon_zip->getClientOriginalName();
  55. //Unzip uploaded update file and remove zip file.
  56. $zip = new ZipArchive;
  57. $res = $zip->open(base_path('public/' . $path));
  58. $random_dir = Str::random(10);
  59. $dir = trim($zip->getNameIndex(0), '/');
  60. if ($res === true) {
  61. $res = $zip->extractTo(base_path('temp/' . $random_dir . '/addons'));
  62. $zip->close();
  63. } else {
  64. dd('could not open');
  65. }
  66. $str = file_get_contents(base_path('temp/' . $random_dir . '/addons/' . $dir . '/config.json'));
  67. $json = json_decode($str, true);
  68. //dd($random_dir, $json);
  69. if (BusinessSetting::where('type', 'current_version')->first()->value >= $json['minimum_item_version']) {
  70. if (count(Addon::where('unique_identifier', $json['unique_identifier'])->get()) == 0) {
  71. $addon = new Addon;
  72. $addon->name = $json['name'];
  73. $addon->unique_identifier = $json['unique_identifier'];
  74. $addon->version = $json['version'];
  75. $addon->activated = 1;
  76. $addon->image = $json['addon_banner'];
  77. $addon->purchase_code = $request->purchase_code;
  78. $addon->save();
  79. // Create new directories.
  80. if (!empty($json['directory'])) {
  81. //dd($json['directory'][0]['name']);
  82. foreach ($json['directory'][0]['name'] as $directory) {
  83. if (is_dir(base_path($directory)) == false) {
  84. mkdir(base_path($directory), 0777, true);
  85. } else {
  86. echo "error on creating directory";
  87. }
  88. }
  89. }
  90. // Create/Replace new files.
  91. if (!empty($json['files'])) {
  92. foreach ($json['files'] as $file) {
  93. copy(base_path('temp/' . $random_dir . '/' . $file['root_directory']), base_path($file['update_directory']));
  94. }
  95. }
  96. // Run sql modifications
  97. $sql_path = base_path('temp/' . $random_dir . '/addons/' . $dir . '/sql/update.sql');
  98. if (file_exists($sql_path)) {
  99. DB::unprepared(file_get_contents($sql_path));
  100. }
  101. flash(translate('Addon installed successfully'))->success();
  102. return redirect()->route('addons.index');
  103. } else {
  104. // Create new directories.
  105. if (!empty($json['directory'])) {
  106. //dd($json['directory'][0]['name']);
  107. foreach ($json['directory'][0]['name'] as $directory) {
  108. if (is_dir(base_path($directory)) == false) {
  109. mkdir(base_path($directory), 0777, true);
  110. } else {
  111. echo "error on creating directory";
  112. }
  113. }
  114. }
  115. // Create/Replace new files.
  116. if (!empty($json['files'])) {
  117. foreach ($json['files'] as $file) {
  118. copy(base_path('temp/' . $random_dir . '/' . $file['root_directory']), base_path($file['update_directory']));
  119. }
  120. }
  121. $addon = Addon::where('unique_identifier', $json['unique_identifier'])->first();
  122. for ($i = $addon->version + 0.05; $i <= $json['version']; $i = $i + 0.1) {
  123. // Run sql modifications
  124. $sql_version = $i+0.05;
  125. $sql_path = base_path('temp/' . $random_dir . '/addons/' . $dir . '/sql/' . $sql_version . '.sql');
  126. if (file_exists($sql_path)) {
  127. DB::unprepared(file_get_contents($sql_path));
  128. }
  129. }
  130. $addon->version = $json['version'];
  131. $addon->purchase_code = $request->purchase_code;
  132. $addon->save();
  133. flash(translate('This addon is updated successfully'))->success();
  134. return redirect()->route('addons.index');
  135. }
  136. } else {
  137. flash(translate('This version is not capable of installing Addons, Please update.'))->error();
  138. return redirect()->route('addons.index');
  139. }
  140. }
  141. }
  142. else {
  143. flash(translate('Please enable ZipArchive extension.'))->error();
  144. return back();
  145. }
  146. }
  147. /**
  148. * Display the specified resource.
  149. *
  150. * @param \App\Models\Addon $addon
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function show(Addon $addon)
  154. {
  155. //
  156. }
  157. public function list()
  158. {
  159. //return view('backend.'.Auth::user()->role.'.addon.list')->render();
  160. }
  161. /**
  162. * Show the form for editing the specified resource.
  163. *
  164. * @param \App\Models\Addon $addon
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function edit(Addon $addon)
  168. {
  169. //
  170. }
  171. /**
  172. * Update the specified resource in storage.
  173. *
  174. * @param \Illuminate\Http\Request $request
  175. * @param \App\Models\Addon $addon
  176. * @return \Illuminate\Http\Response
  177. */
  178. public function update(Request $request, $id)
  179. {
  180. }
  181. /**
  182. * Remove the specified resource from storage.
  183. *
  184. * @param \App\Models\Addon $addon
  185. * @return \Illuminate\Http\Response
  186. */
  187. public function activation(Request $request)
  188. {
  189. if (env('DEMO_MODE') == 'On') {
  190. flash(translate('This action is disabled in demo mode'))->error();
  191. return 0;
  192. }
  193. $addon = Addon::find($request->id);
  194. $addon->activated = $request->status;
  195. $addon->save();
  196. Cache::forget('addons');
  197. return 1;
  198. }
  199. }