PageRenderTime 60ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/workflows.php

https://github.com/lucciano/alfred-dev-doctor
PHP | 447 lines | 256 code | 46 blank | 145 comment | 44 complexity | d4af9855407bc0010ede2a27368bbaed MD5 | raw file
  1. <?php
  2. /**
  3. * Name: Workflows
  4. * Description: This PHP class object provides several useful functions for retrieving, parsing,
  5. * and formatting data to be used with Alfred 2 Workflows.
  6. * Author: David Ferguson (@jdfwarrior)
  7. * Revised: 1/24/2013
  8. * Version: 0.1
  9. */
  10. class Workflows {
  11. private $cache;
  12. private $data;
  13. private $bundle;
  14. private $path;
  15. private $home;
  16. private $results;
  17. /**
  18. * Description:
  19. * Class constructor function. Intializes all class variables. Accepts one optional parameter
  20. * of the workflow bundle id in the case that you want to specify a different bundle id. This
  21. * would adjust the output directories for storing data.
  22. *
  23. * @param $bundleid - optional bundle id if not found automatically
  24. * @return none
  25. */
  26. function __construct( $bundleid=null )
  27. {
  28. $this->path = exec('pwd');
  29. $this->home = exec('printf $HOME');
  30. if ( file_exists( 'info.plist' ) ):
  31. $this->bundle = $this->get( 'bundleid', 'info.plist' );
  32. endif;
  33. if ( !is_null( $bundleid ) ):
  34. $this->bundle = $bundleid;
  35. endif;
  36. $this->cache = $this->home. "/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/".$this->bundle;
  37. $this->data = $this->home. "/Library/Application Support/Alfred 2/Workflow Data/".$this->bundle;
  38. if ( !file_exists( $this->cache ) ):
  39. exec("mkdir '".$this->cache."'");
  40. endif;
  41. if ( !file_exists( $this->data ) ):
  42. exec("mkdir '".$this->data."'");
  43. endif;
  44. $this->results = array();
  45. }
  46. /**
  47. * Description:
  48. * Accepts no parameter and returns the value of the bundle id for the current workflow.
  49. * If no value is available, then false is returned.
  50. *
  51. * @param none
  52. * @return false if not available, bundle id value if available.
  53. */
  54. public function bundle()
  55. {
  56. if ( is_null( $this->bundle ) ):
  57. return false;
  58. else:
  59. return $this->bundle;
  60. endif;
  61. }
  62. /**
  63. * Description:
  64. * Accepts no parameter and returns the value of the path to the cache directory for your
  65. * workflow if it is available. Returns false if the value isn't available.
  66. *
  67. * @param none
  68. * @return false if not available, path to the cache directory for your workflow if available.
  69. */
  70. public function cache()
  71. {
  72. if ( is_null( $this->bundle ) ):
  73. return false;
  74. else:
  75. if ( is_null( $this->cache ) ):
  76. return false;
  77. else:
  78. return $this->cache;
  79. endif;
  80. endif;
  81. }
  82. /**
  83. * Description:
  84. * Accepts no parameter and returns the value of the path to the storage directory for your
  85. * workflow if it is available. Returns false if the value isn't available.
  86. *
  87. * @param none
  88. * @return false if not available, path to the storage directory for your workflow if available.
  89. */
  90. public function data()
  91. {
  92. if ( is_null( $this->bundle ) ):
  93. return false;
  94. else:
  95. if ( is_null( $this->data ) ):
  96. return false;
  97. else:
  98. return $this->data;
  99. endif;
  100. endif;
  101. }
  102. /**
  103. * Description:
  104. * Accepts no parameter and returns the value of the path to the current directory for your
  105. * workflow if it is available. Returns false if the value isn't available.
  106. *
  107. * @param none
  108. * @return false if not available, path to the current directory for your workflow if available.
  109. */
  110. public function path()
  111. {
  112. if ( is_null( $this->path ) ):
  113. return false;
  114. else:
  115. return $this->path;
  116. endif;
  117. }
  118. /**
  119. * Description:
  120. * Accepts no parameter and returns the value of the home path for the current user
  121. * Returns false if the value isn't available.
  122. *
  123. * @param none
  124. * @return false if not available, home path for the current user if available.
  125. */
  126. public function home()
  127. {
  128. if ( is_null( $this->home ) ):
  129. return false;
  130. else:
  131. return $this->home;
  132. endif;
  133. }
  134. /**
  135. * Description:
  136. * Returns an array of available result items
  137. *
  138. * @param none
  139. * @return array - list of result items
  140. */
  141. public function results()
  142. {
  143. return $this->results;
  144. }
  145. /**
  146. * Description:
  147. * Convert an associative array into XML format
  148. *
  149. * @param $a - An associative array to convert
  150. * @param $format - format of data being passed (json or array), defaults to array
  151. * @return - XML string representation of the array
  152. */
  153. public function toxml( $a=null, $format='array' ) {
  154. if ( $format == 'json' ):
  155. $a = json_decode( $a, TRUE );
  156. endif;
  157. if ( is_null( $a ) && !empty( $this->results ) ):
  158. $a = $this->results;
  159. elseif ( is_null( $a ) && empty( $this->results ) ):
  160. return false;
  161. endif;
  162. $items = new SimpleXMLElement("<items></items>"); // Create new XML element
  163. foreach( $a as $b ): // Lop through each object in the array
  164. $c = $items->addChild( 'item' ); // Add a new 'item' element for each object
  165. $c_keys = array_keys( $b ); // Grab all the keys for that item
  166. foreach( $c_keys as $key ): // For each of those keys
  167. if ( $key == 'uid' ):
  168. $c->addAttribute( 'uid', $b[$key] );
  169. elseif ( $key == 'arg' ):
  170. $c->addAttribute( 'arg', $b[$key] );
  171. elseif ( $key == 'valid' ):
  172. if ( $b[$key] == 'yes' && $b[$key] == 'no' ):
  173. $c->addAttribute( 'valid', $b[$key] );
  174. endif;
  175. elseif ( $key == 'autocomplete' ):
  176. $c->addAttribute( 'autocomplete', $b[$key] );
  177. else:
  178. $c->$key = $b[$key];
  179. endif;
  180. endforeach;
  181. endforeach;
  182. return $items->asXML(); // Return XML string representation of the array
  183. }
  184. /**
  185. * Description:
  186. * Remove all items from an associative array that do not have a value
  187. *
  188. * @param $a - Associative array
  189. * @return bool
  190. */
  191. private function empty_filter( $a ) {
  192. if ( $a == '' || $a == null ): // if $a is empty or null
  193. return false; // return false, else, return true
  194. else:
  195. return true;
  196. endif;
  197. }
  198. /**
  199. * Description:
  200. * Save values to a specified plist. If the first parameter is an associative
  201. * array, then the second parameter becomes the plist file to save to. If the
  202. * first parameter is string, then it is assumed that the first parameter is
  203. * the label, the second parameter is the value, and the third parameter is
  204. * the plist file to save the data to.
  205. *
  206. * @param $a - associative array of values to save
  207. * @param $b - the value of the setting
  208. * @param $c - the plist to save the values into
  209. * @return string - execution output
  210. */
  211. public function set( $a=null, $b=null, $c=null )
  212. {
  213. if ( is_array( $a ) ):
  214. if ( file_exists( $b ) ):
  215. $b = $this->path."/".$b;
  216. elseif ( file_exists( $this->data."/".$b ) ):
  217. $b = $this->data."/".$b;
  218. elseif ( file_exists( $this->cache."/".$b ) ):
  219. $b = $this->cache."/".$b;
  220. else:
  221. $b = $this->data."/".$b;
  222. endif;
  223. else:
  224. if ( file_exists( $c ) ):
  225. $c = $this->path."/".$c;
  226. elseif ( file_exists( $this->data."/".$c ) ):
  227. $c = $this->data."/".$c;
  228. elseif ( file_exists( $this->cache."/".$c ) ):
  229. $c = $this->cache."/".$c;
  230. else:
  231. $c = $this->data."/".$c;
  232. endif;
  233. endif;
  234. if ( is_array( $a ) ):
  235. foreach( $a as $k => $v ):
  236. exec( 'defaults write "'. $b .'" '. $k .' "'. $v .'"');
  237. endforeach;
  238. else:
  239. exec( 'defaults write "'. $c .'" '. $a .' "'. $b .'"');
  240. endif;
  241. }
  242. /**
  243. * Description:
  244. * Read a value from the specified plist
  245. *
  246. * @param $a - the value to read
  247. * @param $b - plist to read the values from
  248. * @return bool false if not found, string if found
  249. */
  250. public function get( $a, $b ) {
  251. if ( file_exists( $b ) ):
  252. $b = $this->path."/".$b;
  253. elseif ( file_exists( $this->data."/".$b ) ):
  254. $b = $this->data."/".$b;
  255. elseif ( file_exists( $this->cache."/".$b ) ):
  256. $b = $this->cache."/".$b;
  257. else:
  258. return false;
  259. endif;
  260. exec( 'defaults read "'. $b .'" '.$a, $out ); // Execute system call to read plist value
  261. if ( $out == "" ):
  262. return false;
  263. endif;
  264. $out = $out[0];
  265. return $out; // Return item value
  266. }
  267. /**
  268. * Description:
  269. * Read data from a remote file/url, essentially a shortcut for curl
  270. *
  271. * @param $url - URL to request
  272. * @param $options - Array of curl options
  273. * @return result from curl_exec
  274. */
  275. public function request( $url=null, $options=null )
  276. {
  277. if ( is_null( $url ) ):
  278. return false;
  279. endif;
  280. $defaults = array( // Create a list of default curl options
  281. CURLOPT_RETURNTRANSFER => true, // Returns the result as a string
  282. CURLOPT_URL => $url, // Sets the url to request
  283. CURLOPT_FRESH_CONNECT => true
  284. );
  285. if ( $options ):
  286. foreach( $options as $k => $v ):
  287. $defaults[$k] = $v;
  288. endforeach;
  289. endif;
  290. array_filter( $defaults, // Filter out empty options from the array
  291. array( $this, 'empty_filter' ) );
  292. $ch = curl_init(); // Init new curl object
  293. curl_setopt_array( $ch, $defaults ); // Set curl options
  294. $out = curl_exec( $ch ); // Request remote data
  295. $err = curl_error( $ch );
  296. curl_close( $ch ); // End curl request
  297. if ( $err ):
  298. return $err;
  299. else:
  300. return $out;
  301. endif;
  302. }
  303. /**
  304. * Description:
  305. * Allows searching the local hard drive using mdfind
  306. *
  307. * @param $query - search string
  308. * @return array - array of search results
  309. */
  310. public function mdfind( $query )
  311. {
  312. exec('mdfind "'.$query.'"', $results);
  313. return $results;
  314. }
  315. /**
  316. * Description:
  317. * Accepts data and a string file name to store data to local file as cache
  318. *
  319. * @param array - data to save to file
  320. * @param file - filename to write the cache data to
  321. * @return none
  322. */
  323. public function write( $a, $b )
  324. {
  325. if ( file_exists( $b ) ):
  326. $b = $this->path."/".$b;
  327. elseif ( file_exists( $this->data."/".$b ) ):
  328. $b = $this->data."/".$b;
  329. elseif ( file_exists( $this->cache."/".$b ) ):
  330. $b = $this->cache."/".$b;
  331. else:
  332. $b = $this->data."/".$b;
  333. endif;
  334. if ( is_array( $a ) ):
  335. $a = json_encode( $a );
  336. file_put_contents( $b, $a );
  337. return true;
  338. elseif ( is_string( $a ) ):
  339. file_put_contents( $b, $a );
  340. return true;
  341. else:
  342. return false;
  343. endif;
  344. }
  345. /**
  346. * Description:
  347. * Returns data from a local cache file
  348. *
  349. * @param file - filename to read the cache data from
  350. * @return false if the file cannot be found, the file data if found. If the file
  351. * format is json encoded, then a json object is returned.
  352. */
  353. public function read( $a )
  354. {
  355. if ( file_exists( $a ) ):
  356. $a = $this->path."/".$a;
  357. elseif ( file_exists( $this->data."/".$a ) ):
  358. $a = $this->data."/".$a;
  359. elseif ( file_exists( $this->cache."/".$a ) ):
  360. $a = $this->cache."/".$a;
  361. else:
  362. return false;
  363. endif;
  364. $out = file_get_contents( $a );
  365. if ( !is_null( json_decode( $out ) ) ):
  366. $out = json_decode( $out );
  367. endif;
  368. return $out;
  369. }
  370. /**
  371. * Description:
  372. * Helper function that just makes it easier to pass values into a function
  373. * and create an array result to be passed back to Alfred
  374. *
  375. * @param $uid - the uid of the result, should be unique
  376. * @param $arg - the argument that will be passed on
  377. * @param $title - The title of the result item
  378. * @param $sub - The subtitle text for the result item
  379. * @param $icon - the icon to use for the result item
  380. * @param $valid - sets whether the result item can be actioned
  381. * @param $auto - the autocomplete value for the result item
  382. * @return array - array item to be passed back to Alfred
  383. */
  384. public function result( $uid, $arg, $title, $sub, $icon, $valid='yes', $auto=null )
  385. {
  386. if ( is_null( $auto ) ):
  387. $auto = $title;
  388. endif;
  389. $temp = array(
  390. 'uid' => $uid,
  391. 'arg' => $arg,
  392. 'title' => $title,
  393. 'subtitle' => $sub,
  394. 'icon' => $icon,
  395. 'valid' => $valid,
  396. 'autocomplete' => $auto
  397. );
  398. array_push( $this->results, $temp );
  399. return $temp;
  400. }
  401. }