PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/iNews.io/workflows.php

https://github.com/lnsoso/Alfred-Workflows-1
PHP | 472 lines | 281 code | 46 blank | 145 comment | 54 complexity | 8087b82a80be3088d243e7783b420bc4 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: 2/9/2013
  8. * Version: 0.3
  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 == 'type' ):
  172. $c->addAttribute( 'type', $b[$key] );
  173. elseif ( $key == 'valid' ):
  174. if ( $b[$key] == 'yes' || $b[$key] == 'no' ):
  175. $c->addAttribute( 'valid', $b[$key] );
  176. endif;
  177. elseif ( $key == 'autocomplete' ):
  178. $c->addAttribute( 'autocomplete', $b[$key] );
  179. elseif ( $key == 'icon' ):
  180. if ( substr( $b[$key], 0, 9 ) == 'fileicon:' ):
  181. $val = substr( $b[$key], 9 );
  182. $c->$key = $val;
  183. $c->$key->addAttribute( 'type', 'fileicon' );
  184. elseif ( substr( $b[$key], 0, 9 ) == 'filetype:' ):
  185. $val = substr( $b[$key], 9 );
  186. $c->$key = $val;
  187. $c->$key->addAttribute( 'type', 'filetype' );
  188. else:
  189. $c->$key = $b[$key];
  190. endif;
  191. else:
  192. $c->$key = $b[$key];
  193. endif;
  194. endforeach;
  195. endforeach;
  196. return $items->asXML(); // Return XML string representation of the array
  197. }
  198. /**
  199. * Description:
  200. * Remove all items from an associative array that do not have a value
  201. *
  202. * @param $a - Associative array
  203. * @return bool
  204. */
  205. private function empty_filter( $a ) {
  206. if ( $a == '' || $a == null ): // if $a is empty or null
  207. return false; // return false, else, return true
  208. else:
  209. return true;
  210. endif;
  211. }
  212. /**
  213. * Description:
  214. * Save values to a specified plist. If the first parameter is an associative
  215. * array, then the second parameter becomes the plist file to save to. If the
  216. * first parameter is string, then it is assumed that the first parameter is
  217. * the label, the second parameter is the value, and the third parameter is
  218. * the plist file to save the data to.
  219. *
  220. * @param $a - associative array of values to save
  221. * @param $b - the value of the setting
  222. * @param $c - the plist to save the values into
  223. * @return string - execution output
  224. */
  225. public function set( $a=null, $b=null, $c=null )
  226. {
  227. if ( is_array( $a ) ):
  228. if ( file_exists( $b ) ):
  229. if ( file_exists( $this->path.'/'.$b ) ):
  230. $b = $this->path.'/'.$b;
  231. endif;
  232. elseif ( file_exists( $this->data."/".$b ) ):
  233. $b = $this->data."/".$b;
  234. elseif ( file_exists( $this->cache."/".$b ) ):
  235. $b = $this->cache."/".$b;
  236. else:
  237. $b = $this->data."/".$b;
  238. endif;
  239. else:
  240. if ( file_exists( $c ) ):
  241. if ( file_exists( $this->path.'/'.$c ) ):
  242. $c = $this->path.'/'.$c;
  243. endif;
  244. elseif ( file_exists( $this->data."/".$c ) ):
  245. $c = $this->data."/".$c;
  246. elseif ( file_exists( $this->cache."/".$c ) ):
  247. $c = $this->cache."/".$c;
  248. else:
  249. $c = $this->data."/".$c;
  250. endif;
  251. endif;
  252. if ( is_array( $a ) ):
  253. foreach( $a as $k => $v ):
  254. exec( 'defaults write "'. $b .'" '. $k .' "'. $v .'"');
  255. endforeach;
  256. else:
  257. exec( 'defaults write "'. $c .'" '. $a .' "'. $b .'"');
  258. endif;
  259. }
  260. /**
  261. * Description:
  262. * Read a value from the specified plist
  263. *
  264. * @param $a - the value to read
  265. * @param $b - plist to read the values from
  266. * @return bool false if not found, string if found
  267. */
  268. public function get( $a, $b ) {
  269. if ( file_exists( $b ) ):
  270. if ( file_exists( $this->path.'/'.$b ) ):
  271. $b = $this->path.'/'.$b;
  272. endif;
  273. elseif ( file_exists( $this->data."/".$b ) ):
  274. $b = $this->data."/".$b;
  275. elseif ( file_exists( $this->cache."/".$b ) ):
  276. $b = $this->cache."/".$b;
  277. else:
  278. return false;
  279. endif;
  280. exec( 'defaults read "'. $b .'" '.$a, $out ); // Execute system call to read plist value
  281. if ( $out == "" ):
  282. return false;
  283. endif;
  284. $out = $out[0];
  285. return $out; // Return item value
  286. }
  287. /**
  288. * Description:
  289. * Read data from a remote file/url, essentially a shortcut for curl
  290. *
  291. * @param $url - URL to request
  292. * @param $options - Array of curl options
  293. * @return result from curl_exec
  294. */
  295. public function request( $url=null, $options=null )
  296. {
  297. if ( is_null( $url ) ):
  298. return false;
  299. endif;
  300. $defaults = array( // Create a list of default curl options
  301. CURLOPT_RETURNTRANSFER => true, // Returns the result as a string
  302. CURLOPT_URL => $url, // Sets the url to request
  303. CURLOPT_FRESH_CONNECT => true
  304. );
  305. if ( $options ):
  306. foreach( $options as $k => $v ):
  307. $defaults[$k] = $v;
  308. endforeach;
  309. endif;
  310. array_filter( $defaults, // Filter out empty options from the array
  311. array( $this, 'empty_filter' ) );
  312. $ch = curl_init(); // Init new curl object
  313. curl_setopt_array( $ch, $defaults ); // Set curl options
  314. $out = curl_exec( $ch ); // Request remote data
  315. $err = curl_error( $ch );
  316. curl_close( $ch ); // End curl request
  317. if ( $err ):
  318. return $err;
  319. else:
  320. return $out;
  321. endif;
  322. }
  323. /**
  324. * Description:
  325. * Allows searching the local hard drive using mdfind
  326. *
  327. * @param $query - search string
  328. * @return array - array of search results
  329. */
  330. public function mdfind( $query )
  331. {
  332. exec('mdfind "'.$query.'"', $results);
  333. return $results;
  334. }
  335. /**
  336. * Description:
  337. * Accepts data and a string file name to store data to local file as cache
  338. *
  339. * @param array - data to save to file
  340. * @param file - filename to write the cache data to
  341. * @return none
  342. */
  343. public function write( $a, $b )
  344. {
  345. if ( file_exists( $b ) ):
  346. if ( file_exists( $this->path.'/'.$b ) ):
  347. $b = $this->path.'/'.$b;
  348. endif;
  349. elseif ( file_exists( $this->data."/".$b ) ):
  350. $b = $this->data."/".$b;
  351. elseif ( file_exists( $this->cache."/".$b ) ):
  352. $b = $this->cache."/".$b;
  353. else:
  354. $b = $this->data."/".$b;
  355. endif;
  356. if ( is_array( $a ) ):
  357. $a = json_encode( $a );
  358. file_put_contents( $b, $a );
  359. return true;
  360. elseif ( is_string( $a ) ):
  361. file_put_contents( $b, $a );
  362. return true;
  363. else:
  364. return false;
  365. endif;
  366. }
  367. /**
  368. * Description:
  369. * Returns data from a local cache file
  370. *
  371. * @param file - filename to read the cache data from
  372. * @return false if the file cannot be found, the file data if found. If the file
  373. * format is json encoded, then a json object is returned.
  374. */
  375. public function read( $a )
  376. {
  377. if ( file_exists( $a ) ):
  378. if ( file_exists( $this->path.'/'.$a ) ):
  379. $a = $this->path.'/'.$a;
  380. endif;
  381. elseif ( file_exists( $this->data."/".$a ) ):
  382. $a = $this->data."/".$a;
  383. elseif ( file_exists( $this->cache."/".$a ) ):
  384. $a = $this->cache."/".$a;
  385. else:
  386. return false;
  387. endif;
  388. $out = file_get_contents( $a );
  389. if ( !is_null( json_decode( $out ) ) ):
  390. $out = json_decode( $out );
  391. endif;
  392. return $out;
  393. }
  394. /**
  395. * Description:
  396. * Helper function that just makes it easier to pass values into a function
  397. * and create an array result to be passed back to Alfred
  398. *
  399. * @param $uid - the uid of the result, should be unique
  400. * @param $arg - the argument that will be passed on
  401. * @param $title - The title of the result item
  402. * @param $sub - The subtitle text for the result item
  403. * @param $icon - the icon to use for the result item
  404. * @param $valid - sets whether the result item can be actioned
  405. * @param $auto - the autocomplete value for the result item
  406. * @return array - array item to be passed back to Alfred
  407. */
  408. public function result( $uid, $arg, $title, $sub, $icon, $valid='yes', $auto=null, $type=null )
  409. {
  410. $temp = array(
  411. 'uid' => $uid,
  412. 'arg' => $arg,
  413. 'title' => $title,
  414. 'subtitle' => $sub,
  415. 'icon' => $icon,
  416. 'valid' => $valid,
  417. 'autocomplete' => $auto,
  418. 'type' => $type
  419. );
  420. if ( is_null( $type ) ):
  421. unset( $temp['type'] );
  422. endif;
  423. array_push( $this->results, $temp );
  424. return $temp;
  425. }
  426. }