PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/revslider/inc_php/framework/base_admin.class.php

https://gitlab.com/hop23typhu/faci-times
PHP | 526 lines | 280 code | 115 blank | 131 comment | 37 complexity | 1c7ce7d60ae52747e8774100937ed913 MD5 | raw file
  1. <?php
  2. class UniteBaseAdminClassRev extends UniteBaseClassRev{
  3. const ACTION_ADMIN_MENU = "admin_menu";
  4. const ACTION_ADMIN_INIT = "admin_init";
  5. const ACTION_ADD_SCRIPTS = "admin_enqueue_scripts";
  6. const ROLE_ADMIN = "admin";
  7. const ROLE_EDITOR = "editor";
  8. const ROLE_AUTHOR = "author";
  9. protected static $master_view;
  10. protected static $view;
  11. private static $arrSettings = array();
  12. private static $arrMenuPages = array();
  13. private static $tempVars = array();
  14. private static $startupError = "";
  15. private static $menuRole = self::ROLE_ADMIN;
  16. /**
  17. *
  18. * main constructor
  19. */
  20. public function __construct($mainFile,$t,$defaultView){
  21. parent::__construct($mainFile,$t);
  22. //set view
  23. self::$view = self::getGetVar("view");
  24. if(empty(self::$view))
  25. self::$view = $defaultView;
  26. //add internal hook for adding a menu in arrMenus
  27. self::addAction(self::ACTION_ADMIN_MENU, "addAdminMenu");
  28. //if not inside plugin don't continue
  29. if($this->isInsidePlugin() == true){
  30. self::addAction(self::ACTION_ADD_SCRIPTS, "addCommonScripts");
  31. self::addAction(self::ACTION_ADD_SCRIPTS, "onAddScripts");
  32. }
  33. //a must event for any admin. call onActivate function.
  34. $this->addEvent_onActivate();
  35. self::addActionAjax("show_image", "onShowImage");
  36. }
  37. /**
  38. *
  39. * set the menu role - for viewing menus
  40. */
  41. public static function setMenuRole($menuRole){
  42. self::$menuRole = $menuRole;
  43. }
  44. /**
  45. *
  46. * set startup error to be shown in master view
  47. */
  48. public static function setStartupError($errorMessage){
  49. self::$startupError = $errorMessage;
  50. }
  51. /**
  52. *
  53. * tells if the the current plugin opened is this plugin or not
  54. * in the admin side.
  55. */
  56. private function isInsidePlugin(){
  57. $page = self::getGetVar("page");
  58. if($page == self::$dir_plugin)
  59. return(true);
  60. return(false);
  61. }
  62. /**
  63. *
  64. * add common used scripts
  65. */
  66. public static function addCommonScripts(){
  67. //include jquery ui
  68. if(GlobalsRevSlider::$isNewVersion){ //load new jquery ui library
  69. $urlJqueryUI = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js";
  70. self::addScriptAbsoluteUrl($urlJqueryUI,"jquery-ui");
  71. self::addStyle("jquery-ui-1.9.2.custom.min","jui-smoothness","css/jui/new");
  72. if(function_exists("wp_enqueue_media"))
  73. wp_enqueue_media();
  74. }else{ //load old jquery ui library
  75. $urlJqueryUI = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js";
  76. self::addScriptAbsoluteUrl($urlJqueryUI,"jquery-ui");
  77. self::addStyle("jquery-ui-1.8.18.custom","jui-smoothness","css/jui/old");
  78. }
  79. self::addScriptCommon("settings","unite_settings");
  80. self::addScriptCommon("admin","unite_admin");
  81. self::addScriptCommon("jquery.tipsy","tipsy");
  82. //--- add styles
  83. self::addStyleCommon("admin","unite_admin");
  84. //add tipsy
  85. self::addStyleCommon("tipsy","tipsy");
  86. //include farbtastic
  87. self::addScriptCommon("my-farbtastic","my-farbtastic","js/farbtastic");
  88. self::addStyleCommon("farbtastic","farbtastic","js/farbtastic");
  89. //include codemirror
  90. self::addScriptCommon("codemirror","codemirror_js","js/codemirror");
  91. self::addScriptCommon("css","codemirror_js_css","js/codemirror");
  92. self::addStyleCommon("codemirror","codemirror_css","js/codemirror");
  93. //include dropdown checklist
  94. self::addScriptCommon("ui.dropdownchecklist-1.4-min","dropdownchecklist_js","js/dropdownchecklist");
  95. //self::addScriptCommon("ui.dropdownchecklist","dropdownchecklist_js","js/dropdownchecklist");
  96. //self::addStyleCommon("ui.dropdownchecklist.standalone","dropdownchecklist_css","js/dropdownchecklist");
  97. }
  98. /**
  99. *
  100. * admin pages parent, includes all the admin files by default
  101. */
  102. public static function adminPages(){
  103. //self::validateAdminPermissions();
  104. }
  105. /**
  106. *
  107. * validate permission that the user is admin, and can manage options.
  108. */
  109. protected static function isAdminPermissions(){
  110. if( is_admin() && current_user_can("manage_options") )
  111. return(true);
  112. return(false);
  113. }
  114. /**
  115. *
  116. * validate admin permissions, if no pemissions - exit
  117. */
  118. protected static function validateAdminPermissions(){
  119. if(!self::isAdminPermissions()){
  120. echo "access denied";
  121. return(false);
  122. }
  123. }
  124. /**
  125. *
  126. * set view that will be the master
  127. */
  128. protected static function setMasterView($masterView){
  129. self::$master_view = $masterView;
  130. }
  131. /**
  132. *
  133. * inlcude some view file
  134. */
  135. protected static function requireView($view){
  136. try{
  137. //require master view file, and
  138. if(!empty(self::$master_view) && !isset(self::$tempVars["is_masterView"]) ){
  139. $masterViewFilepath = self::$path_views.self::$master_view.".php";
  140. UniteFunctionsRev::validateFilepath($masterViewFilepath,"Master View");
  141. self::$tempVars["is_masterView"] = true;
  142. require $masterViewFilepath;
  143. }
  144. else{ //simple require the view file.
  145. $viewFilepath = self::$path_views.$view.".php";
  146. UniteFunctionsRev::validateFilepath($viewFilepath,"View");
  147. require $viewFilepath;
  148. }
  149. }catch (Exception $e){
  150. echo "<br><br>View ($view) Error: <b>".$e->getMessage()."</b>";
  151. if(self::$debugMode == true)
  152. dmp($e->getTraceAsString());
  153. }
  154. }
  155. /**
  156. * require some template from "templates" folder
  157. */
  158. protected static function getPathTemplate($templateName){
  159. $pathTemplate = self::$path_templates.$templateName.".php";
  160. UniteFunctionsRev::validateFilepath($pathTemplate,"Template");
  161. return($pathTemplate);
  162. }
  163. /**
  164. *
  165. * require settings file, the filename without .php
  166. */
  167. protected static function requireSettings($settingsFile){
  168. try{
  169. require self::$path_plugin."settings/$settingsFile.php";
  170. }catch (Exception $e){
  171. echo "<br><br>Settings ($settingsFile) Error: <b>".$e->getMessage()."</b>";
  172. dmp($e->getTraceAsString());
  173. }
  174. }
  175. /**
  176. *
  177. * get path to settings file
  178. * @param $settingsFile
  179. */
  180. protected static function getSettingsFilePath($settingsFile){
  181. $filepath = self::$path_plugin."settings/$settingsFile.php";
  182. return($filepath);
  183. }
  184. /**
  185. *
  186. * add all js and css needed for media upload
  187. */
  188. protected static function addMediaUploadIncludes(){
  189. self::addWPScript("thickbox");
  190. self::addWPStyle("thickbox");
  191. self::addWPScript("media-upload");
  192. }
  193. /**
  194. * add admin menus from the list.
  195. */
  196. public static function addAdminMenu(){
  197. $role = "manage_options";
  198. switch(self::$menuRole){
  199. case self::ROLE_AUTHOR:
  200. $role = "edit_published_posts";
  201. break;
  202. case self::ROLE_EDITOR:
  203. $role = "edit_pages";
  204. break;
  205. default:
  206. case self::ROLE_ADMIN:
  207. $role = "manage_options";
  208. break;
  209. }
  210. foreach(self::$arrMenuPages as $menu){
  211. $title = $menu["title"];
  212. $pageFunctionName = $menu["pageFunction"];
  213. add_menu_page( $title, $title, $role, self::$dir_plugin, array(self::$t, $pageFunctionName) );
  214. }
  215. }
  216. /**
  217. *
  218. * add menu page
  219. */
  220. protected static function addMenuPage($title,$pageFunctionName){
  221. self::$arrMenuPages[] = array("title"=>$title,"pageFunction"=>$pageFunctionName);
  222. }
  223. /**
  224. *
  225. * get url to some view.
  226. */
  227. public static function getViewUrl($viewName,$urlParams=""){
  228. $params = "&view=".$viewName;
  229. if(!empty($urlParams))
  230. $params .= "&".$urlParams;
  231. $link = admin_url( "admin.php?page=".self::$dir_plugin.$params);
  232. return($link);
  233. }
  234. /**
  235. *
  236. * register the "onActivate" event
  237. */
  238. protected function addEvent_onActivate($eventFunc = "onActivate"){
  239. register_activation_hook( self::$mainFile, array(self::$t, $eventFunc) );
  240. }
  241. /**
  242. *
  243. * store settings in the object
  244. */
  245. protected static function storeSettings($key,$settings){
  246. self::$arrSettings[$key] = $settings;
  247. }
  248. /**
  249. *
  250. * get settings object
  251. */
  252. protected static function getSettings($key){
  253. if(!isset(self::$arrSettings[$key]))
  254. UniteFunctionsRev::throwError("Settings $key not found");
  255. $settings = self::$arrSettings[$key];
  256. return($settings);
  257. }
  258. /**
  259. *
  260. * add ajax back end callback, on some action to some function.
  261. */
  262. protected static function addActionAjax($ajaxAction,$eventFunction){
  263. self::addAction('wp_ajax_'.self::$dir_plugin."_".$ajaxAction, $eventFunction);
  264. self::addAction('wp_ajax_nopriv_'.self::$dir_plugin."_".$ajaxAction, $eventFunction);
  265. }
  266. /**
  267. *
  268. * echo json ajax response
  269. */
  270. private static function ajaxResponse($success,$message,$arrData = null){
  271. $response = array();
  272. $response["success"] = $success;
  273. $response["message"] = $message;
  274. if(!empty($arrData)){
  275. if(gettype($arrData) == "string")
  276. $arrData = array("data"=>$arrData);
  277. $response = array_merge($response,$arrData);
  278. }
  279. $json = json_encode($response);
  280. echo $json;
  281. exit();
  282. }
  283. /**
  284. *
  285. * echo json ajax response, without message, only data
  286. */
  287. protected static function ajaxResponseData($arrData){
  288. if(gettype($arrData) == "string")
  289. $arrData = array("data"=>$arrData);
  290. self::ajaxResponse(true,"",$arrData);
  291. }
  292. /**
  293. *
  294. * echo json ajax response
  295. */
  296. protected static function ajaxResponseError($message,$arrData = null){
  297. self::ajaxResponse(false,$message,$arrData,true);
  298. }
  299. /**
  300. * echo ajax success response
  301. */
  302. protected static function ajaxResponseSuccess($message,$arrData = null){
  303. self::ajaxResponse(true,$message,$arrData,true);
  304. }
  305. /**
  306. * echo ajax success response
  307. */
  308. protected static function ajaxResponseSuccessRedirect($message,$url){
  309. $arrData = array("is_redirect"=>true,"redirect_url"=>$url);
  310. self::ajaxResponse(true,$message,$arrData,true);
  311. }
  312. /**
  313. *
  314. * Enter description here ...
  315. */
  316. protected static function updatePlugin($viewBack = false){
  317. $linkBack = self::getViewUrl($viewBack);
  318. $htmlLinkBack = UniteFunctionsRev::getHtmlLink($linkBack, "Go Back");
  319. $zip = new UniteZipRev();
  320. try{
  321. if(function_exists("unzip_file") == false){
  322. if( UniteZipRev::isZipExists() == false)
  323. UniteFunctionsRev::throwError("The ZipArchive php extension not exists, can't extract the update file. Please turn it on in php ini.");
  324. }
  325. dmp("Update in progress...");
  326. $arrFiles = UniteFunctionsRev::getVal($_FILES, "update_file");
  327. if(empty($arrFiles))
  328. UniteFunctionsRev::throwError("Update file don't found.");
  329. $filename = UniteFunctionsRev::getVal($arrFiles, "name");
  330. if(empty($filename))
  331. UniteFunctionsRev::throwError("Update filename not found.");
  332. $fileType = UniteFunctionsRev::getVal($arrFiles, "type");
  333. /*
  334. $fileType = strtolower($fileType);
  335. if($fileType != "application/zip")
  336. UniteFunctionsRev::throwError("The file uploaded is not zip.");
  337. */
  338. $filepathTemp = UniteFunctionsRev::getVal($arrFiles, "tmp_name");
  339. if(file_exists($filepathTemp) == false)
  340. UniteFunctionsRev::throwError("Can't find the uploaded file.");
  341. //crate temp folder
  342. UniteFunctionsRev::checkCreateDir(self::$path_temp);
  343. //create the update folder
  344. $pathUpdate = self::$path_temp."update_extract/";
  345. UniteFunctionsRev::checkCreateDir($pathUpdate);
  346. //remove all files in the update folder
  347. if(is_dir($pathUpdate)){
  348. $arrNotDeleted = UniteFunctionsRev::deleteDir($pathUpdate,false);
  349. if(!empty($arrNotDeleted)){
  350. $strNotDeleted = print_r($arrNotDeleted,true);
  351. UniteFunctionsRev::throwError("Could not delete those files from the update folder: $strNotDeleted");
  352. }
  353. }
  354. //copy the zip file.
  355. $filepathZip = $pathUpdate.$filename;
  356. $success = move_uploaded_file($filepathTemp, $filepathZip);
  357. if($success == false)
  358. UniteFunctionsRev::throwError("Can't move the uploaded file here: {$filepathZip}.");
  359. if(function_exists("unzip_file") == true){
  360. WP_Filesystem();
  361. $response = unzip_file($filepathZip, $pathUpdate);
  362. }
  363. else
  364. $zip->extract($filepathZip, $pathUpdate);
  365. //get extracted folder
  366. $arrFolders = UniteFunctionsRev::getFoldersList($pathUpdate);
  367. if(empty($arrFolders))
  368. UniteFunctionsRev::throwError("The update folder is not extracted");
  369. if(count($arrFolders) > 1)
  370. UniteFunctionsRev::throwError("Extracted folders are more then 1. Please check the update file.");
  371. //get product folder
  372. $productFolder = $arrFolders[0];
  373. if(empty($productFolder))
  374. UniteFunctionsRev::throwError("Wrong product folder.");
  375. if($productFolder != self::$dir_plugin)
  376. UniteFunctionsRev::throwError("The update folder don't match the product folder, please check the update file.");
  377. $pathUpdateProduct = $pathUpdate.$productFolder."/";
  378. //check some file in folder to validate it's the real one:
  379. $checkFilepath = $pathUpdateProduct.$productFolder.".php";
  380. if(file_exists($checkFilepath) == false)
  381. UniteFunctionsRev::throwError("Wrong update extracted folder. The file: {$checkFilepath} not found.");
  382. //copy the plugin without the captions file.
  383. //$pathOriginalPlugin = $pathUpdate."copy/";
  384. $pathOriginalPlugin = self::$path_plugin;
  385. $arrBlackList = array();
  386. $arrBlackList[] = "rs-plugin/css/captions.css";
  387. UniteFunctionsRev::copyDir($pathUpdateProduct, $pathOriginalPlugin,"",$arrBlackList);
  388. //delete the update
  389. UniteFunctionsRev::deleteDir($pathUpdate);
  390. dmp("Updated Successfully, redirecting...");
  391. echo "<script>location.href='$linkBack'</script>";
  392. }catch(Exception $e){
  393. $message = $e->getMessage();
  394. $message .= " <br> Please update the plugin manually via the ftp";
  395. echo "<div style='color:#B80A0A;font-size:18px;'><b>Update Error: </b> $message</div><br>";
  396. echo $htmlLinkBack;
  397. exit();
  398. }
  399. }
  400. }
  401. ?>