plugin.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. // PLUGIN INFORMATION
  3. $GLOBALS['plugins']['Bookmark'] = array( // Plugin Name
  4. 'name' => 'Bookmark', // Plugin Name
  5. 'author' => 'leet1994', // Who wrote the plugin
  6. 'category' => 'Utilities', // One to Two Word Description
  7. 'link' => '', // Link to plugin info
  8. 'license' => 'personal,business', // License Type use , for multiple
  9. 'idPrefix' => 'BOOKMARK', // html element id prefix
  10. 'configPrefix' => 'BOOKMARK', // config file prefix for array items without the hyphen
  11. 'dbPrefix' => 'BOOKMARK', // db prefix
  12. 'version' => '0.1.0', // SemVer of plugin
  13. 'image' => 'api/plugins/bookmark/logo.png', // 1:1 non transparent image for plugin
  14. 'settings' => true, // does plugin need a settings modal?
  15. 'bind' => true, // use default bind to make settings page - true or false
  16. 'api' => 'api/v2/plugins/bookmark/settings', // api route for settings page
  17. 'homepage' => false // Is plugin for use on homepage? true or false
  18. );
  19. // Logo image under Public Domain from https://openclipart.org/detail/182527/open-book
  20. class Bookmark extends Organizr
  21. {
  22. public function writeLog($type = 'error', $message = null, $username = null)
  23. {
  24. parent::writeLog($type, "Plugin 'Bookmark': " . $message, $username);
  25. }
  26. public function _bookmarkGetOrganizrTabInfo()
  27. {
  28. $response = [
  29. array(
  30. 'function' => 'fetch',
  31. 'query' => array(
  32. 'SELECT * FROM tabs',
  33. 'WHERE url = ?',
  34. 'api/v2/plugins/bookmark/page'
  35. )
  36. ),
  37. ];
  38. return $this->processQueries($response);
  39. }
  40. public function _bookmarkGetOrganizrTabGroupId()
  41. {
  42. $tab = $this->_bookmarkGetOrganizrTabInfo();
  43. if ($tab) {
  44. return $tab['group_id'];
  45. } else {
  46. return 999;
  47. }
  48. }
  49. public function _checkRequest($request)
  50. {
  51. $result = false;
  52. if ($this->config['BOOKMARK-enabled'] && $this->hasDB()) {
  53. if (!$this->_checkDatabaseTablesExist()) {
  54. $this->_createDatabaseTables();
  55. }
  56. $result = true;
  57. }
  58. return $result;
  59. }
  60. public function _checkDatabaseTablesExist()
  61. {
  62. if ($this->config['driver'] == 'sqlite3') {
  63. $queryCategories = ["SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' AND `name` = 'BOOKMARK-categories'"];
  64. $queryTabs = ["SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' AND `name` = 'BOOKMARK-tabs'"];
  65. } else {
  66. $queryCategories = ['SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_TYPE LIKE "BASE TABLE" AND TABLE_NAME = %s', (string)$this->config['dbName'], 'BOOKMARK-categories'];
  67. $queryTabs = ['SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_TYPE LIKE "BASE TABLE" AND TABLE_NAME = %s', (string)$this->config['dbName'], 'BOOKMARK-categories'];
  68. }
  69. $response = [
  70. array(
  71. 'function' => 'fetchSingle',
  72. 'query' => $queryCategories,
  73. 'key' => 'BOOKMARK-categories'
  74. ),
  75. array(
  76. 'function' => 'fetchSingle',
  77. 'query' => $queryTabs,
  78. 'key' => 'BOOKMARK-tabs'
  79. ),
  80. ];
  81. $data = $this->processQueries($response);
  82. return ($data["BOOKMARK-categories"] != false && $data["BOOKMARK-tabs"] != false);
  83. }
  84. protected function _createDatabaseTables()
  85. {
  86. $response = [
  87. array(
  88. 'function' => 'query',
  89. 'query' => 'CREATE TABLE `BOOKMARK-categories` (
  90. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  91. `order` INTEGER,
  92. `category` TEXT UNIQUE,
  93. `category_id` INTEGER,
  94. `default` INTEGER
  95. );'
  96. ),
  97. array(
  98. 'function' => 'query',
  99. 'query' => 'CREATE TABLE `BOOKMARK-tabs` (
  100. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  101. `order` INTEGER,
  102. `category_id` INTEGER,
  103. `name` TEXT,
  104. `url` TEXT,
  105. `enabled` INTEGER,
  106. `group_id` INTEGER,
  107. `image` TEXT,
  108. `background_color` TEXT,
  109. `text_color` TEXT
  110. );'
  111. )
  112. ];
  113. $this->processQueries($response);
  114. }
  115. public function _getSettings()
  116. {
  117. return array(
  118. 'custom' => '
  119. <div class="row">
  120. <div class="col-lg-6 col-sm-12 col-md-6">
  121. <div class="white-box">
  122. <h3 class="box-title" lang="en">Automatic Setup Tasks</h3>
  123. <ul class="feeds">
  124. <li class="bookmark-check-tab">
  125. <div class="bg-info">
  126. <i class="sticon ti-layout-tab-v text-white"></i>
  127. </div>
  128. <small lang="en">Checking for Bookmark tab...</small>
  129. <span class="text-muted result"><i class="fa fa-spin fa-refresh"></i></span>
  130. </li>
  131. <li class="bookmark-check-category">
  132. <div class="bg-success">
  133. <i class="ti-layout-list-thumb text-white"></i>
  134. </div>
  135. <small lang="en">Checking for bookmark default category...</small>
  136. <span class="text-muted result"><i class="fa fa-spin fa-refresh"></i></span>
  137. </li>
  138. </ul>
  139. </div>
  140. </div>
  141. <div class="col-lg-6 col-sm-12 col-md-6">
  142. <div class="panel panel-info">
  143. <div class="panel-heading">
  144. <span lang="en">Notice</span>
  145. </div>
  146. <div class="panel-wrapper collapse in" aria-expanded="true">
  147. <div class="panel-body">
  148. <ul class="list-icons">
  149. <li><i class="fa fa-chevron-right text-info"></i> <span lang="en">Add tab that points to <i>api/v2/plugins/bookmark/page</i> and set it\'s type to <i>Organizr</i>.</span></li>
  150. <li><i class="fa fa-chevron-right text-info"></i> <span lang="en">Create Bookmark categories in the new area in <i>Tab Editor</i>.</span></li>
  151. <li><i class="fa fa-chevron-right text-info"></i> <span lang="en">Create Bookmark tabs in the new area in <i>Tab Editor</i>.</span></li>
  152. <li><i class="fa fa-chevron-right text-info"></i> <span lang="en">Open your custom Bookmark page via menu.</span></li>
  153. </ul>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. </div>
  159. '
  160. );
  161. }
  162. public function _getPage()
  163. {
  164. $bookmarks = '<div id="BOOKMARK-wrapper">';
  165. foreach ($this->_getAllCategories() as $category) {
  166. $tabs = $this->_getRelevantTabsForCategory($category['category_id']);
  167. if (count($tabs) == 0) continue;
  168. $bookmarks .= '<div class="BOOKMARK-category">
  169. <div class="BOOKMARK-category-title">
  170. ' . $category['category'] . '
  171. </div>
  172. <div class="BOOKMARK-category-content">';
  173. foreach ($tabs as $tab) {
  174. $bookmarks .= '<a href="' . $tab['url'] . '" target="_SELF">
  175. <div class="BOOKMARK-tab"
  176. style="border-color: ' . $this->adjustBrightness($tab['background_color'], 0.3) . '; background: linear-gradient(90deg, ' . $this->adjustBrightness($tab['background_color'], -0.3) . ' 0%, ' . $tab['background_color'] . ' 70%, ' . $this->adjustBrightness($tab['background_color'], 0.1) . ' 100%);">
  177. <span class="BOOKMARK-tab-image">' . $this->_iconPrefix($tab['image']) . '</span>
  178. <span class="BOOKMARK-tab-title" style="color: ' . $tab['text_color'] . ';">' . $tab['name'] . '</span>
  179. </div>
  180. </a>';
  181. }
  182. $bookmarks .= '</div></div>';
  183. }
  184. $bookmarks .= '</div>';
  185. return $bookmarks;
  186. }
  187. protected function _iconPrefix($source)
  188. {
  189. $tabIcon = explode("::", $source);
  190. $icons = array(
  191. "materialize" => "mdi mdi-",
  192. "fontawesome" => "fa fa-",
  193. "themify" => "ti-",
  194. "simpleline" => "icon-",
  195. "weathericon" => "wi wi-",
  196. "alphanumeric" => "fa-fw",
  197. );
  198. if (is_array($tabIcon) && count($tabIcon) == 2) {
  199. if ($tabIcon[0] !== 'url' && $tabIcon[0] !== 'alphanumeric') {
  200. return '<i class="' . $icons[$tabIcon[0]] . $tabIcon[1] . '"></i>';
  201. } else if ($tabIcon[0] == 'alphanumeric') {
  202. return '<i>' . $tabIcon[1] . '</i>';
  203. } else {
  204. return '<img src="' . $tabIcon[1] . '" alt="tabIcon" />';
  205. }
  206. } else {
  207. return '<img src="' . $source . '" alt="tabIcon" />';
  208. }
  209. }
  210. protected function _getAllCategories()
  211. {
  212. $response = [
  213. array(
  214. 'function' => 'fetchAll',
  215. 'query' => 'SELECT * FROM `BOOKMARK-categories` ORDER BY `order` ASC'
  216. )
  217. ];
  218. return $this->processQueries($response);
  219. }
  220. protected function _getRelevantTabsForCategory($category_id)
  221. {
  222. $response = [
  223. array(
  224. 'function' => 'fetchAll',
  225. 'query' => array(
  226. "SELECT * FROM `BOOKMARK-tabs` WHERE `enabled`='1' AND `category_id`=? AND `group_id`>=? ORDER BY `order` ASC",
  227. $category_id,
  228. $this->getUserLevel()
  229. )
  230. )
  231. ];
  232. return $this->processQueries($response);
  233. }
  234. public function _getTabs()
  235. {
  236. $response = [
  237. array(
  238. 'function' => 'fetchAll',
  239. 'query' => 'SELECT * FROM `BOOKMARK-tabs` ORDER BY `order` ASC',
  240. 'key' => 'tabs'
  241. ),
  242. array(
  243. 'function' => 'fetchAll',
  244. 'query' => 'SELECT * FROM `BOOKMARK-categories` ORDER BY `order` ASC',
  245. 'key' => 'categories'
  246. ),
  247. array(
  248. 'function' => 'fetchAll',
  249. 'query' => 'SELECT * FROM `groups` ORDER BY `group_id` ASC',
  250. 'key' => 'groups'
  251. )
  252. ];
  253. return $this->processQueries($response);
  254. }
  255. // Tabs
  256. public function _getSettingsTabEditorBookmarkTabsPage()
  257. {
  258. $iconSelectors = '
  259. $(".bookmarkTabIconIconList").select2({
  260. ajax: {
  261. url: \'api/v2/icon\',
  262. data: function (params) {
  263. var query = {
  264. search: params.term,
  265. page: params.page || 1
  266. }
  267. return query;
  268. },
  269. processResults: function (data, params) {
  270. params.page = params.page || 1;
  271. return {
  272. results: data.response.data.results,
  273. pagination: {
  274. more: (params.page * 20) < data.response.data.total
  275. }
  276. };
  277. },
  278. //cache: true
  279. },
  280. placeholder: \'Search for an icon\',
  281. templateResult: formatIcon,
  282. templateSelection: formatIcon
  283. });
  284. $(".bookmarkTabIconImageList").select2({
  285. ajax: {
  286. url: \'api/v2/image/select\',
  287. data: function (params) {
  288. var query = {
  289. search: params.term,
  290. page: params.page || 1
  291. }
  292. return query;
  293. },
  294. processResults: function (data, params) {
  295. params.page = params.page || 1;
  296. return {
  297. results: data.response.data.results,
  298. pagination: {
  299. more: (params.page * 20) < data.response.data.total
  300. }
  301. };
  302. },
  303. //cache: true
  304. },
  305. placeholder: \'Search for an image\',
  306. templateResult: formatImage,
  307. templateSelection: formatImage
  308. });
  309. ';
  310. return '
  311. <script>
  312. buildBookmarkTabEditor();
  313. !function(a){function f(a,b){if(!(a.originalEvent.touches.length>1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery);
  314. $( \'#bookmarkTabEditorTable\' ).sortable({
  315. stop: function () {
  316. $(\'input.order\').each(function(idx) {
  317. $(this).val(idx + 1);
  318. });
  319. var newTabs = $( "#submit-bookmark-tabs-form" ).serializeToJSON();
  320. newBookmarkTabsGlobal = newTabs;
  321. $(\'.saveBookmarkTabOrderButton\').removeClass(\'hidden\');
  322. //submitTabOrder(newTabs);
  323. }
  324. });
  325. $( \'#bookmarkTabEditorTable\' ).disableSelection();
  326. ' . $iconSelectors . '
  327. </script>
  328. <div class="panel bg-org panel-info">
  329. <div class="panel-heading">
  330. <span lang="en">Bookmark Tab Editor</span>
  331. <button type="button" class="btn btn-info btn-circle pull-right popup-with-form m-r-5" href="#new-bookmark-tab-form" onclick="newBookmarkTabForm()" data-effect="mfp-3d-unfold"><i class="fa fa-plus"></i> </button>
  332. <button onclick="submitBookmarkTabOrder(newBookmarkTabsGlobal)" class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right animated loop-animation rubberBand m-r-20 saveBookmarkTabOrderButton hidden" type="button"><span class="btn-label"><i class="fa fa-save"></i></span><span lang="en">Save Tab Order</span></button>
  333. </div>
  334. <div class="table-responsive">
  335. <form id="submit-bookmark-tabs-form" onsubmit="return false;">
  336. <table class="table table-hover manage-u-table">
  337. <thead>
  338. <tr>
  339. <th width="70" class="text-center">#</th>
  340. <th lang="en">NAME</th>
  341. <th lang="en">CATEGORY</th>
  342. <th lang="en">GROUP</th>
  343. <th lang="en" style="text-align:center">ACTIVE</th>
  344. <th lang="en" style="text-align:center">EDIT</th>
  345. <th lang="en" style="text-align:center">DELETE</th>
  346. </tr>
  347. </thead>
  348. <tbody id="bookmarkTabEditorTable">
  349. <td class="text-center" colspan="12"><i class="fa fa-spin fa-spinner"></i></td>
  350. </tbody>
  351. </table>
  352. </form>
  353. </div>
  354. </div>
  355. <form id="new-bookmark-tab-form" class="mfp-hide white-popup-block mfp-with-anim">
  356. <h1 lang="en">Add New Tab</h1>
  357. <fieldset style="border:0;">
  358. <div class="form-group">
  359. <label class="control-label" for="new-bookmark-tab-form-inputName" lang="en">Tab Name</label>
  360. <input type="text" class="form-control" id="new-bookmark-tab-form-inputName" name="name" required="" autofocus>
  361. </div>
  362. <div class="form-group">
  363. <label class="control-label" for="new-bookmark-tab-form-inputURL" lang="en">Tab URL</label>
  364. <input type="text" class="form-control" id="new-bookmark-tab-form-inputURL" name="url" required="">
  365. </div>
  366. <div class="row">
  367. <div class="form-group col-lg-4">
  368. <label class="control-label" for="new-bookmark-tab-form-chooseImage" lang="en">Choose Image</label>
  369. <select class="form-control bookmarkTabIconImageList" id="new-bookmark-tab-form-chooseImage" name="chooseImage"><option lang="en">Select or type Image</option></select>
  370. </div>
  371. <div class="form-group col-lg-4">
  372. <label class="control-label" for="new-bookmark-tab-form-chooseIcon" lang="en">Choose Icon</label>
  373. <select class="form-control bookmarkTabIconIconList" id="new-bookmark-tab-form-chooseIcon" name="chooseIcon"><option lang="en">Select or type Icon</option></select>
  374. </div>
  375. <div class="form-group col-lg-4">
  376. <label class="control-label" for="new-bookmark-tab-form-chooseBlackberry" lang="en">Choose Blackberry Theme Icon</label>
  377. <button id="new-bookmark-tab-form-chooseBlackberry" class="btn btn-xs btn-primary waves-effect waves-light form-control" onclick="showBlackberryThemes(\'new-bookmark-tab-form-inputImageNew\');" type="button">
  378. <i class="fa fa-search"></i>&nbsp; <span lang="en">Choose</span>
  379. </button>
  380. </div>
  381. </div>
  382. <div class="form-group">
  383. <label class="control-label" for="new-bookmark-tab-form-inputImage" lang="en">Tab Image</label>
  384. <input type="text" class="form-control" id="new-bookmark-tab-form-inputImage" name="image" required="">
  385. </div>
  386. <div class="row">
  387. <div class="form-group col-lg-4">
  388. <label class="control-label" for="new-bookmark-tab-form-inputBackgroundColor" lang="en">Background Color</label>
  389. <input type="text" class="form-control bookmark-pick-a-color" id="new-bookmark-tab-form-inputBackgroundColor" name="background_color" required="" value="#fff">
  390. </div>
  391. <div class="form-group col-lg-4">
  392. <label class="control-label" for="new-bookmark-tab-form-inputTextColor" lang="en">Text Color</label>
  393. <input type="text" class="form-control bookmark-pick-a-color" id="new-bookmark-tab-form-inputTextColor" name="text_color" required="" value="#000">
  394. </div>
  395. <div class="form-group col-lg-4">
  396. <label class="control-label" for="new-bookmark-preview" lang="en">Preview</label>
  397. <div id="new-bookmark-preview"></div>
  398. </div>
  399. </div>
  400. </fieldset>
  401. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none addNewBookmarkTab" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Add Tab</span></button>
  402. <div class="clearfix"></div>
  403. </form>
  404. <form id="edit-bookmark-tab-form" class="mfp-hide white-popup-block mfp-with-anim">
  405. <input type="hidden" name="id" value="x">
  406. <span class="hidden" id="originalBookmarkTabName"></span>
  407. <h1 lang="en">Edit Tab</h1>
  408. <fieldset style="border:0;">
  409. <div class="form-group">
  410. <label class="control-label" for="edit-bookmark-tab-form-inputName" lang="en">Tab Name</label>
  411. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputName" name="name" required="" autofocus>
  412. </div>
  413. <div class="form-group">
  414. <label class="control-label" for="edit-bookmark-tab-form-inputURL" lang="en">Tab URL</label>
  415. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputURL" name="url" required="">
  416. </div>
  417. <div class="row">
  418. <div class="form-group col-lg-4">
  419. <label class="control-label" for="edit-bookmark-tab-form-chooseImage" lang="en">Choose Image</label>
  420. <select class="form-control bookmarkTabIconImageList" id="edit-bookmark-tab-form-chooseImage" name="chooseImage"><option lang="en">Select or type Image</option></select>
  421. </div>
  422. <div class="form-group col-lg-4">
  423. <label class="control-label" for="edit-bookmark-tab-form-chooseIcon" lang="en">Choose Icon</label>
  424. <select class="form-control bookmarkTabIconIconList" id="edit-bookmark-tab-form-chooseIcon" name="chooseIcon"><option lang="en">Select or type Icon</option></select>
  425. </div>
  426. <div class="form-group col-lg-4">
  427. <label class="control-label" for="edit-bookmark-tab-form-chooseBlackberry" lang="en">Choose Blackberry Theme Icon</label>
  428. <button id="edit-bookmark-tab-form-chooseBlackberry" class="btn btn-xs btn-primary waves-effect waves-light form-control" onclick="showBlackberryThemes(\'edit-bookmark-tab-form-inputImage\');" type="button">
  429. <i class="fa fa-search"></i>&nbsp; <span lang="en">Choose</span>
  430. </button>
  431. </div>
  432. </div>
  433. <div class="form-group">
  434. <label class="control-label" for="edit-bookmark-tab-form-inputImage" lang="en">Tab Image</label>
  435. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputImage" name="image" required="">
  436. </div>
  437. <div class="row">
  438. <div class="form-group col-lg-4">
  439. <label class="control-label" for="edit-bookmark-tab-form-inputBackgroundColor" lang="en">Background Color</label>
  440. <input type="text" class="form-control bookmark-pick-a-color" id="edit-bookmark-tab-form-inputBackgroundColor" name="background_color" required="">
  441. </div>
  442. <div class="form-group col-lg-4">
  443. <label class="control-label" for="edit-bookmark-tab-form-inputTextColor" lang="en">Text Color</label>
  444. <input type="text" class="form-control bookmark-pick-a-color" id="edit-bookmark-tab-form-inputTextColor" name="text_color" required="">
  445. </div>
  446. <div class="form-group col-lg-4">
  447. <label class="control-label" for="edit-bookmark-preview" lang="en">Preview</label>
  448. <div id="edit-bookmark-preview"></div>
  449. </div>
  450. </div>
  451. </fieldset>
  452. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none editBookmarkTab" type="button"><span class="btn-label"><i class="fa fa-check"></i></span><span lang="en">Edit Tab</span></button>
  453. <div class="clearfix"></div>
  454. </form>
  455. ';
  456. }
  457. public function _isBookmarkTabNameTaken($name, $id = null)
  458. {
  459. if ($id) {
  460. $response = [
  461. array(
  462. 'function' => 'fetchAll',
  463. 'query' => array(
  464. 'SELECT * FROM `BOOKMARK-tabs` WHERE `name` LIKE ? AND `id` != ?',
  465. $name,
  466. $id
  467. )
  468. ),
  469. ];
  470. } else {
  471. $response = [
  472. array(
  473. 'function' => 'fetchAll',
  474. 'query' => array(
  475. 'SELECT * FROM `BOOKMARK-tabs` WHERE `name` LIKE ?',
  476. $name
  477. )
  478. ),
  479. ];
  480. }
  481. return $this->processQueries($response);
  482. }
  483. public function _getNextBookmarkTabOrder()
  484. {
  485. $response = [
  486. array(
  487. 'function' => 'fetchSingle',
  488. 'query' => array(
  489. 'SELECT `order` from `BOOKMARK-tabs` ORDER BY `order` DESC'
  490. )
  491. ),
  492. ];
  493. return $this->processQueries($response);
  494. }
  495. public function _getBookmarkTabById($id)
  496. {
  497. $response = [
  498. array(
  499. 'function' => 'fetch',
  500. 'query' => array(
  501. 'SELECT * FROM `BOOKMARK-tabs` WHERE `id` = ?',
  502. $id
  503. )
  504. ),
  505. ];
  506. return $this->processQueries($response);
  507. }
  508. public function _getTabByIdCheckUser($id)
  509. {
  510. $tabInfo = $this->_getBookmarkTabById($id);
  511. if ($tabInfo) {
  512. if ($this->qualifyRequest($tabInfo['group_id'], true)) {
  513. return $tabInfo;
  514. }
  515. } else {
  516. $this->setAPIResponse('error', 'id not found', 404);
  517. return false;
  518. }
  519. }
  520. public function _deleteTab($id)
  521. {
  522. $response = [
  523. array(
  524. 'function' => 'query',
  525. 'query' => array(
  526. 'DELETE FROM `BOOKMARK-tabs` WHERE id = ?',
  527. $id
  528. )
  529. ),
  530. ];
  531. $tabInfo = $this->_getBookmarkTabById($id);
  532. if ($tabInfo) {
  533. $this->writeLog('success', 'Tab Delete Function - Deleted Tab [' . $tabInfo['name'] . ']', $this->user['username']);
  534. $this->setAPIResponse('success', 'Tab deleted', 204);
  535. return $this->processQueries($response);
  536. } else {
  537. $this->setAPIResponse('error', 'id not found', 404);
  538. return false;
  539. }
  540. }
  541. public function _addTab($array)
  542. {
  543. if (!$array) {
  544. $this->setAPIResponse('error', 'no data was sent', 422);
  545. return null;
  546. }
  547. $array = $this->checkKeys($this->getTableColumnsFormatted('BOOKMARK-tabs'), $array);
  548. $array['group_id'] = ($array['group_id']) ?? $this->getDefaultGroupId();
  549. $array['category_id'] = ($array['category_id']) ?? $this->_getDefaultBookmarkCategoryId();
  550. $array['enabled'] = ($array['enabled']) ?? 0;
  551. $array['order'] = ($array['order']) ?? $this->_getNextBookmarkTabOrder() + 1;
  552. if (array_key_exists('name', $array)) {
  553. if ($this->_isBookmarkTabNameTaken($array['name'])) {
  554. $this->setAPIResponse('error', 'Tab name: ' . $array['name'] . ' is already taken', 409);
  555. return false;
  556. }
  557. } else {
  558. $this->setAPIResponse('error', 'Tab name was not supplied', 422);
  559. return false;
  560. }
  561. if (!array_key_exists('url', $array)) {
  562. $this->setAPIResponse('error', 'Tab url was not supplied', 422);
  563. return false;
  564. }
  565. if (!array_key_exists('image', $array)) {
  566. $this->setAPIResponse('error', 'Tab image was not supplied', 422);
  567. return false;
  568. }
  569. if (array_key_exists('background_color', $array)) {
  570. if (!$this->_checkColorHexCode($array['background_color'])) {
  571. $this->setAPIResponse('error', 'Tab background color is invalid', 422);
  572. return false;
  573. }
  574. } else {
  575. $this->setAPIResponse('error', 'Tab background color was not supplied', 422);
  576. return false;
  577. }
  578. if (array_key_exists('text_color', $array)) {
  579. if (!$this->_checkColorHexCode($array['text_color'])) {
  580. $this->setAPIResponse('error', 'Tab text color is invalid', 422);
  581. return false;
  582. }
  583. } else {
  584. $this->setAPIResponse('error', 'Tab text color was not supplied', 422);
  585. return false;
  586. }
  587. $response = [
  588. array(
  589. 'function' => 'query',
  590. 'query' => array(
  591. 'INSERT INTO [BOOKMARK-tabs]',
  592. $array
  593. )
  594. ),
  595. ];
  596. $this->setAPIResponse(null, 'Tab added');
  597. $this->writeLog('success', 'Tab Editor Function - Added Tab for [' . $array['name'] . ']', $this->user['username']);
  598. return $this->processQueries($response);
  599. }
  600. public function _updateTab($id, $array)
  601. {
  602. if (!$id || $id == '') {
  603. $this->setAPIResponse('error', 'id was not set', 422);
  604. return null;
  605. }
  606. if (!$array) {
  607. $this->setAPIResponse('error', 'no data was sent', 422);
  608. return null;
  609. }
  610. $tabInfo = $this->_getBookmarkTabById($id);
  611. if ($tabInfo) {
  612. $array = $this->checkKeys($tabInfo, $array);
  613. } else {
  614. $this->setAPIResponse('error', 'No tab info found', 404);
  615. return false;
  616. }
  617. if (array_key_exists('name', $array)) {
  618. if ($this->_isBookmarkTabNameTaken($array['name'], $id)) {
  619. $this->setAPIResponse('error', 'Tab name: ' . $array['name'] . ' is already taken', 409);
  620. return false;
  621. }
  622. }
  623. if (array_key_exists('background_color', $array)) {
  624. if (!$this->_checkColorHexCode($array['background_color'])) {
  625. $this->setAPIResponse('error', 'Tab background color is invalid', 422);
  626. return false;
  627. }
  628. }
  629. if (array_key_exists('text_color', $array)) {
  630. if (!$this->_checkColorHexCode($array['text_color'])) {
  631. $this->setAPIResponse('error', 'Tab text color is invalid', 422);
  632. return false;
  633. }
  634. }
  635. $response = [
  636. array(
  637. 'function' => 'query',
  638. 'query' => array(
  639. 'UPDATE `BOOKMARK-tabs` SET',
  640. $array,
  641. 'WHERE id = ?',
  642. $id
  643. )
  644. ),
  645. ];
  646. $this->setAPIResponse(null, 'Tab info updated');
  647. $this->writeLog('success', 'Tab Editor Function - Edited Tab Info for [' . $tabInfo['name'] . ']', $this->user['username']);
  648. return $this->processQueries($response);
  649. }
  650. public function _updateTabOrder($array)
  651. {
  652. if (count($array) >= 1) {
  653. foreach ($array as $tab) {
  654. if (count($tab) !== 2) {
  655. $this->setAPIResponse('error', 'data is malformed', 422);
  656. break;
  657. }
  658. $id = $tab['id'] ?? null;
  659. $order = $tab['order'] ?? null;
  660. if ($id && $order) {
  661. $response = [
  662. array(
  663. 'function' => 'query',
  664. 'query' => array(
  665. 'UPDATE `BOOKMARK-tabs` set `order` = ? WHERE `id` = ?',
  666. $order,
  667. $id
  668. )
  669. ),
  670. ];
  671. $this->processQueries($response);
  672. $this->setAPIResponse(null, 'Tab Order updated');
  673. } else {
  674. $this->setAPIResponse('error', 'data is malformed', 422);
  675. }
  676. }
  677. } else {
  678. $this->setAPIResponse('error', 'data is empty or not in array', 422);
  679. return false;
  680. }
  681. }
  682. // Categories
  683. public function _getSettingsTabEditorBookmarkCategoriesPage()
  684. {
  685. return '
  686. <script>
  687. buildBookmarkCategoryEditor();
  688. $( \'#bookmarkCategoryEditorTable\' ).sortable({
  689. stop: function () {
  690. var inputs = $(\'input.order\');
  691. var nbElems = inputs.length;
  692. inputs.each(function(idx) {
  693. $(this).val(idx + 1);
  694. });
  695. submitBookmarkCategoryOrder();
  696. }
  697. });
  698. </script>
  699. <div class="panel bg-org panel-info">
  700. <div class="panel-heading">
  701. <span lang="en">Bookmark Category Editor</span>
  702. <button type="button" class="btn btn-info btn-circle pull-right popup-with-form m-r-5" href="#new-bookmark-category-form" data-effect="mfp-3d-unfold"><i class="fa fa-plus"></i> </button>
  703. </div>
  704. <div class="table-responsive">
  705. <form id="submit-bookmark-categories-form" onsubmit="return false;">
  706. <table class="table table-hover manage-u-table">
  707. <thead>
  708. <tr>
  709. <th lang="en">NAME</th>
  710. <th lang="en" style="text-align:center">TABS</th>
  711. <th lang="en" style="text-align:center">DEFAULT</th>
  712. <th lang="en" style="text-align:center">EDIT</th>
  713. <th lang="en" style="text-align:center">DELETE</th>
  714. </tr>
  715. </thead>
  716. <tbody id="bookmarkCategoryEditorTable"><td class="text-center" colspan="6"><i class="fa fa-spin fa-spinner"></i></td></tbody>
  717. </table>
  718. </form>
  719. </div>
  720. </div>
  721. <form id="new-bookmark-category-form" class="mfp-hide white-popup-block mfp-with-anim">
  722. <h1 lang="en">Add New Bookmark Category</h1>
  723. <fieldset style="border:0;">
  724. <div class="form-group">
  725. <label class="control-label" for="new-bookmark-category-form-inputName" lang="en">Category Name</label>
  726. <input type="text" class="form-control" id="new-bookmark-category-form-inputName" name="category" required="" autofocus>
  727. </div>
  728. </fieldset>
  729. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none addNewBookmarkCategory" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Add Category</span></button>
  730. <div class="clearfix"></div>
  731. </form>
  732. <form id="edit-bookmark-category-form" class="mfp-hide white-popup-block mfp-with-anim">
  733. <input type="hidden" name="id" value="">
  734. <h1 lang="en">Edit Category</h1>
  735. <fieldset style="border:0;">
  736. <div class="form-group">
  737. <label class="control-label" for="edit-bookmark-category-form-inputName" lang="en">Category Name</label>
  738. <input type="text" class="form-control" id="edit-bookmark-category-form-inputName" name="category" required="" autofocus>
  739. </div>
  740. </fieldset>
  741. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none editBookmarkCategory" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Edit Category</span></button>
  742. <div class="clearfix"></div>
  743. </form>
  744. ';
  745. }
  746. public function _getDefaultBookmarkCategoryId()
  747. {
  748. $response = [
  749. array(
  750. 'function' => 'fetchSingle',
  751. 'query' => array(
  752. 'SELECT `category_id` FROM `BOOKMARK-categories` WHERE `default` = 1'
  753. )
  754. ),
  755. ];
  756. return $this->processQueries($response);
  757. }
  758. public function _getNextBookmarkCategoryOrder()
  759. {
  760. $response = [
  761. array(
  762. 'function' => 'fetchSingle',
  763. 'query' => array(
  764. 'SELECT `order` from `BOOKMARK-categories` ORDER BY `order` DESC'
  765. )
  766. ),
  767. ];
  768. return $this->processQueries($response);
  769. }
  770. public function _getNextBookmarkCategoryId()
  771. {
  772. $response = [
  773. array(
  774. 'function' => 'fetchSingle',
  775. 'query' => array(
  776. 'SELECT `category_id` from `BOOKMARK-categories` ORDER BY `category_id` DESC'
  777. )
  778. ),
  779. ];
  780. return $this->processQueries($response);
  781. }
  782. public function _isBookmarkCategoryNameTaken($name, $id = null)
  783. {
  784. if ($id) {
  785. $response = [
  786. array(
  787. 'function' => 'fetchAll',
  788. 'query' => array(
  789. 'SELECT * FROM `BOOKMARK-categories` WHERE `category` LIKE ? AND `id` != ?',
  790. $name,
  791. $id
  792. )
  793. ),
  794. ];
  795. } else {
  796. $response = [
  797. array(
  798. 'function' => 'fetchAll',
  799. 'query' => array(
  800. 'SELECT * FROM `BOOKMARK-categories` WHERE `category` LIKE ?',
  801. $name
  802. )
  803. ),
  804. ];
  805. }
  806. return $this->processQueries($response);
  807. }
  808. public function _getBookmarkCategoryById($id)
  809. {
  810. $response = [
  811. array(
  812. 'function' => 'fetch',
  813. 'query' => array(
  814. 'SELECT * FROM `BOOKMARK-categories` WHERE `id` = ?',
  815. $id
  816. )
  817. ),
  818. ];
  819. return $this->processQueries($response);
  820. }
  821. public function _clearBookmarkCategoryDefault()
  822. {
  823. $response = [
  824. array(
  825. 'function' => 'query',
  826. 'query' => array(
  827. 'UPDATE `BOOKMARK-categories` SET `default` = 0'
  828. )
  829. ),
  830. ];
  831. return $this->processQueries($response);
  832. }
  833. public function _addCategory($array)
  834. {
  835. if (!$array) {
  836. $this->setAPIResponse('error', 'no data was sent', 422);
  837. return null;
  838. }
  839. $array = $this->checkKeys($this->getTableColumnsFormatted('BOOKMARK-categories'), $array);
  840. $array['default'] = ($array['default']) ?? 0;
  841. $array['order'] = ($array['order']) ?? $this->_getNextBookmarkCategoryOrder() + 1;
  842. $array['category_id'] = ($array['category_id']) ?? $this->_getNextBookmarkCategoryId() + 1;
  843. if (array_key_exists('category', $array)) {
  844. if ($this->_isBookmarkCategoryNameTaken($array['category'])) {
  845. $this->setAPIResponse('error', 'Category name: ' . $array['category'] . ' is already taken', 409);
  846. return false;
  847. }
  848. } else {
  849. $this->setAPIResponse('error', 'Category name was not supplied', 422);
  850. return false;
  851. }
  852. $response = [
  853. array(
  854. 'function' => 'query',
  855. 'query' => array(
  856. 'INSERT INTO [BOOKMARK-categories]',
  857. $array
  858. )
  859. ),
  860. ];
  861. $this->setAPIResponse(null, 'Category added');
  862. $this->writeLog('success', 'Category Editor Function - Added Category for [' . $array['category'] . ']', $this->user['username']);
  863. $result = $this->processQueries($response);
  864. $this->_correctDefaultCategory();
  865. return $result;
  866. }
  867. public function _updateCategory($id, $array)
  868. {
  869. if (!$id || $id == '') {
  870. $this->setAPIResponse('error', 'id was not set', 422);
  871. return null;
  872. }
  873. if (!$array) {
  874. $this->setAPIResponse('error', 'no data was sent', 422);
  875. return null;
  876. }
  877. $categoryInfo = $this->_getBookmarkCategoryById($id);
  878. if ($categoryInfo) {
  879. $array = $this->checkKeys($categoryInfo, $array);
  880. } else {
  881. $this->setAPIResponse('error', 'No category info found', 404);
  882. return false;
  883. }
  884. if (array_key_exists('category', $array)) {
  885. if ($this->_isBookmarkCategoryNameTaken($array['category'], $id)) {
  886. $this->setAPIResponse('error', 'Category name: ' . $array['category'] . ' is already taken', 409);
  887. return false;
  888. }
  889. }
  890. if (array_key_exists('default', $array)) {
  891. if ($array['default']) {
  892. $this->_clearBookmarkCategoryDefault();
  893. }
  894. }
  895. $response = [
  896. array(
  897. 'function' => 'query',
  898. 'query' => array(
  899. 'UPDATE `BOOKMARK-categories` SET',
  900. $array,
  901. 'WHERE id = ?',
  902. $id
  903. )
  904. ),
  905. ];
  906. $this->setAPIResponse(null, 'Category info updated');
  907. $this->writeLog('success', 'Category Editor Function - Edited Category Info for [' . $categoryInfo['category'] . ']', $this->user['username']);
  908. $result = $this->processQueries($response);
  909. $this->_correctDefaultCategory();
  910. return $result;
  911. }
  912. public function _updateCategoryOrder($array)
  913. {
  914. if (count($array) >= 1) {
  915. foreach ($array as $category) {
  916. if (count($category) !== 2) {
  917. $this->setAPIResponse('error', 'data is malformed', 422);
  918. break;
  919. }
  920. $id = $category['id'] ?? null;
  921. $order = $category['order'] ?? null;
  922. if ($id && $order) {
  923. $response = [
  924. array(
  925. 'function' => 'query',
  926. 'query' => array(
  927. 'UPDATE `BOOKMARK-categories` set `order` = ? WHERE `id` = ?',
  928. $order,
  929. $id
  930. )
  931. ),
  932. ];
  933. $this->processQueries($response);
  934. $this->setAPIResponse(null, 'Category Order updated');
  935. } else {
  936. $this->setAPIResponse('error', 'data is malformed', 422);
  937. }
  938. }
  939. } else {
  940. $this->setAPIResponse('error', 'data is empty or not in array', 422);
  941. return false;
  942. }
  943. }
  944. public function _deleteCategory($id)
  945. {
  946. $response = [
  947. array(
  948. 'function' => 'query',
  949. 'query' => array(
  950. 'DELETE FROM `BOOKMARK-categories` WHERE id = ?',
  951. $id
  952. )
  953. ),
  954. ];
  955. $categoryInfo = $this->_getBookmarkCategoryById($id);
  956. if ($categoryInfo) {
  957. $this->writeLog('success', 'Category Delete Function - Deleted Category [' . $categoryInfo['category'] . ']', $this->user['username']);
  958. $this->setAPIResponse('success', 'Category deleted', 204);
  959. $result = $this->processQueries($response);
  960. $this->_correctDefaultCategory();
  961. return $result;
  962. } else {
  963. $this->setAPIResponse('error', 'id not found', 404);
  964. return false;
  965. }
  966. }
  967. protected function _correctDefaultCategory()
  968. {
  969. if ($this->_getDefaultBookmarkCategoryId() == null) {
  970. $response = [
  971. array(
  972. 'function' => 'query',
  973. 'query' => 'UPDATE `BOOKMARK-categories` SET `default` = 1 WHERE `category_id` = (SELECT `category_id` FROM `BOOKMARK-categories` ORDER BY `category_id` ASC LIMIT 0,1)'
  974. )
  975. ];
  976. return $this->processQueries($response);
  977. }
  978. }
  979. protected function _checkColorHexCode($hex)
  980. {
  981. return preg_match('/^\#([0-9a-fA-F]{3}){1,2}$/', $hex);
  982. }
  983. /**
  984. * Increases or decreases the brightness of a color by a percentage of the current brightness.
  985. *
  986. * @param string $hexCode Supported formats: `#FFF`, `#FFFFFF`, `FFF`, `FFFFFF`
  987. * @param float $adjustPercent A number between -1 and 1. E.g. 0.3 = 30% lighter; -0.4 = 40% darker.
  988. *
  989. * @return string
  990. *
  991. * @author maliayas
  992. * @link https://stackoverflow.com/questions/3512311/how-to-generate-lighter-darker-color-with-php
  993. */
  994. protected function adjustBrightness($hexCode, $adjustPercent)
  995. {
  996. $hexCode = ltrim($hexCode, '#');
  997. if (strlen($hexCode) == 3) {
  998. $hexCode = $hexCode[0] . $hexCode[0] . $hexCode[1] . $hexCode[1] . $hexCode[2] . $hexCode[2];
  999. }
  1000. $hexCode = array_map('hexdec', str_split($hexCode, 2));
  1001. foreach ($hexCode as &$color) {
  1002. $adjustableLimit = $adjustPercent < 0 ? $color : 255 - $color;
  1003. $adjustAmount = ceil($adjustableLimit * $adjustPercent);
  1004. $color = str_pad(dechex($color + $adjustAmount), 2, '0', STR_PAD_LEFT);
  1005. }
  1006. return '#' . implode($hexCode);
  1007. }
  1008. public function _checkForBookmarkTab()
  1009. {
  1010. $response = [
  1011. array(
  1012. 'function' => 'fetchAll',
  1013. 'query' => array(
  1014. 'SELECT * FROM tabs',
  1015. 'WHERE url = ?',
  1016. 'api/v2/plugins/bookmark/page'
  1017. )
  1018. ),
  1019. ];
  1020. $tab = $this->processQueries($response);
  1021. if ($tab) {
  1022. $this->setAPIResponse('success', 'Tab already exists', 200);
  1023. return $tab;
  1024. } else {
  1025. $createTab = $this->_createBookmarkTab();
  1026. if ($createTab) {
  1027. $tab = $this->processQueries($response);
  1028. $this->setAPIResponse('success', 'Tab created', 200);
  1029. return $tab;
  1030. } else {
  1031. $this->setAPIResponse('error', 'Tab creation error', 500);
  1032. }
  1033. }
  1034. }
  1035. public function _createBookmarkTab()
  1036. {
  1037. $tabInfo = [
  1038. 'order' => $this->getNextTabOrder() + 1,
  1039. 'category_id' => $this->getDefaultCategoryId(),
  1040. 'name' => 'Bookmarks',
  1041. 'url' => 'api/v2/plugins/bookmark/page',
  1042. 'default' => false,
  1043. 'enabled' => true,
  1044. 'group_id' => $this->getDefaultGroupId(),
  1045. 'image' => 'fontawesome::book',
  1046. 'type' => 0
  1047. ];
  1048. $response = [
  1049. array(
  1050. 'function' => 'query',
  1051. 'query' => array(
  1052. 'INSERT INTO [tabs]',
  1053. $tabInfo
  1054. )
  1055. ),
  1056. ];
  1057. return $this->processQueries($response);
  1058. }
  1059. public function _checkForBookmarkCategories()
  1060. {
  1061. $categories = $this->_getAllCategories();
  1062. if ($categories) {
  1063. $this->setAPIResponse('success', 'Categories already exists', 200);
  1064. return $categories;
  1065. } else {
  1066. $createCategory = $this->_addCategory(['category' => 'Unsorted', 'default' => 1]);
  1067. if ($createCategory) {
  1068. $categories = $this->_getAllCategories();
  1069. $this->setAPIResponse('success', 'Category created', 200);
  1070. return $categories;
  1071. } else {
  1072. $this->setAPIResponse('error', 'Category creation error', 500);
  1073. }
  1074. }
  1075. }
  1076. }