bookmark.php 36 KB

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